如果在[表1.keys]中找到四个停止字之一,我想询问如何改进此工作代码以跳过[I]的执行。
for i in table_t1.keys():
if i.find("data") == -1:
if i.find("split") == -1:
if i.find("loss") == -1:
if i.find("prob") == -1:
#do something
不需要使用find函数。
最佳答案
定义单词,如下所示
words = ("data", "split", "loss", "prob")
现在,您可以使用
all
或any
函数,如下所示if all(word not in i for word in words):
...
if not any(word in i for word in words):
...
它们基本上检查了
words
中是否存在i
元组的词。注意:如果
table_t1
实际上是一个字典,那么您不必调用.keys
(它将创建一个键列表)。你可以像这样简单地迭代它for i in table_t1: