词频分析
不可以输入符号
"""
功能:词频分析
作者:cxf
日期: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 words_count in zip(diff_words, counts):
print(words_count)