希望简化if语句中的多个变量

时间:2016-10-08 23:17:05

标签: python variables if-statement

我试图简化这段代码:

for word in wordlist:
    if (word != 'a' and
        word != 'i' and
        word != 'and' and
        word != 'this' and
        word != 'is' and
        word != 'in'):

进入

commom_words = ['a', 'i', 'and', 'this', 'is', 'in]

for word in wordlist:
   if word != any(common_words):

我也试过all(common_words)

使用原始代码,我在搜索文本时省略了常用词。当我试图在变量中简化它们时,如果声明的话,常见的单词就会通过。

1 个答案:

答案 0 :(得分:0)

最简单的方法可能是使用not in语法:

common_words = ['a', 'i', 'and', 'this', 'is', 'in']
for word in wordlist:
    if word not in common_words:
        # Do something with it...