我是靠谱客的博主 清脆夕阳,这篇文章主要介绍python实现统计文本中单词出现的频率,现在分享给大家,希望可以做个参考。

#coding=utf-8
import os
from collections import Counter
sumsdata=[]
for fname in os.listdir(os.getcwd()):
    if os.path.isfile(fname) and fname.endswith('.txt'):
        with open(fname,'r') as fp:
            data=fp.readlines()
        sumsdata+=[line.strip().lower() for line in data]
cnt=Counter()
for word in sumsdata:
    cnt[word]+=1
cnt=dict(cnt)
for key,value in cnt.items():
    print(key+":"+str(value))

首先在和程序所在路径下创建几个文本文件,我建了两个,文件内容分别为hello python goodbye python 和 i like python。运行程序,得到以下结果

最后

以上就是清脆夕阳最近收集整理的关于python实现统计文本中单词出现的频率的全部内容,更多相关python实现统计文本中单词出现内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部