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)
#第二种方法-利用Collections模块的Counter来进行词频统计
text="I Love you python and I Love you java and I love you"
diff_text=text.split(" ")#去掉重复
from collections import Counter#利用Collections模块的Counter来进行词频统计
x=Counter(diff_text)#遍历单词列表,统计各个单词的个数
for key in x.keys():#进行循环 .是代表的的意思
print("{}:{}".format(key,x[key]))
**不负青云志 常怀赤子心**


最后
以上就是明理可乐最近收集整理的关于利用Collections模块的Counter来进行词频统计的全部内容,更多相关利用Collections模块内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复