我有一个单词列表,想知道其中有多少个不同的单词。我最终会将列表导入到字典中,显示每个单词的数量。
现在我有
while i < len(list_words):
if list_words[i] in list_words:
repetitions += 1
i += 1
print(repetitions)
但这只是返回列表的长度。
请您参考如下方法:
试试这个,
word_counts = dict.fromkeys(list_words, 0)
for word in list_words:
word_counts[word] += 1
