使用列表写一个有很多异常的 if 行

write a if line with a lot of exception using list

我在 if 行中有很多异常,如下所示:

if "Aide" not in title and "Accessibilité" not in title and "iphone" not in title and "android" not in title and "windows" not in title and "applications" not in title and "RSS:" not in title:
    do_stuff()

如何编写此行以使用列表?

我试过:

for a in ["Aide", "Accessibilité", "iphone" , "android", "windows", "applications", "RSS:"]:
   if title != a:
      do_stuff()

但是此方法为每个 a 调用 do_stuff(),所以这不是我想要的...

我该怎么做?谢谢

你可以这样写:

def contains_any(s, it):
    return any(word in s for word in it)

if not contains_any(title, ["Aide", "Accessibilité", "iphone" , "android",
                            "windows", "applications", "RSS:"]):
    ...

根据 jonrsharpe 的建议,您可以这样做:

titleList = ["Aide", "Accessibilite", "iphone" , "android", "windows", "applications", "RSS:"]
if all(title != x for x in titleList):
     do_stuff()

编辑:

或者,这要简单得多(是 Tanveer Alam 指出的):

if title not in titleList:
     do_stuff()

为什么我一开始不把它写出来...可能需要一些非常认真的反省。