私はこのコードを単純化しようとしました:
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)
。
元のコードを使用して、一般的な単語はテキストの検索から省略されました。変数でそれらを単純化しようとしたとき、一般的な単語はifステートメントを通り抜けていました。
最も簡単なアプローチは、おそらく次のnot in
構文を使用することです。
common_words = ['a', 'i', 'and', 'this', 'is', 'in']
for word in wordlist:
if word not in common_words:
# Do something with it...
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加