I have an array (list) of words. I want to loop though them and check if each one is in the English dictionary. If it is I want to increase a variable by 1.
如果我没听错,你可以这样做:
wordlist = ["foo", "bar", .....]
dictionary = …
words_in_dictionary = len([word for word in wordlist if word in dictionary])
This is all very simple apart from the 'getting Python to read a dictionary' which I'm not sure about. I have found a .txt dictionary which I could use but the words are in there with definitions making it impossible to pick out the actual words.
from collections import defaultdict
words = ['foo', 'bar', 'baz']
wordlist = open('linuxwords.txt').read().splitlines()
d = defaultdict(bool)
for word in words:
d[word] = word in wordlist
for word in d:
print "Does the word '{0}' appear in the English dictionary: {1}".format(word, "yes" if d[word] else "no")
如果我没听错,你可以这样做:
{cd2}你可以从unix中获得更好的单词。如果你把那本字典的一部分贴出来,我可以帮你。在
一旦你选择了一个合适的文件来创建你的单词表,你就可以使用^{} 来跟踪你的列表中的单词是否出现在英语词典中。在
结果:
^{pr2}$相关问题 更多 >
编程相关推荐