我试图简化这段代码:
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)
。
使用原始代码,我在搜索文本时省略了常用词。当我试图在变量中简化它们时,如果声明的话,常见的单词就会通过。
答案 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...