内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用
我想用python查看某个单词是否在英语字典中。
怎么做呢?
def is_english_word(word): pass # how to I implement is_english_word? is_english_word(token.lower())
使用一个集合来存储单词列表,因为查找它们会更快:
with open("english_words.txt") as word_file: english_words = set(word.strip().lower() for word in word_file) def is_english_word(word): return word.lower() in english_words print is_english_word("ham") # should be true if you have a good english_words.txt