Python词频分析

词频分析

不可以输入符号

"""
功能:词频分析
作者: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)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_62590351/article/details/121538902
今日推荐