词频统计
编写程序
'''
功能:词频统计
作者:hf
日期:2021.11.25
'''
text = 'I love python I love java I learn python'
# 拆分
words = text.split(' ')
# 去重
diff_words = list(set(words))
# 统计单词个数的列表
counts = []
for i in range(len(diff_words)):
counts.append(0)
# 遍历单词列表,统计各个单词的个数
for i in range(len(words)):
for j in range(len(diff_words)):
if diff_words[j] == words[i]:
counts[j] = counts[j] + 1
# 输出统计结果
for word_count in zip(diff_words, counts):
print(word_count)
运行程序,查看结果