首页 > 其他 > 详细

综合练习:词频统计

时间:2018-03-27 18:21:57      阅读:214      评论:0      收藏:0      [点我收藏+]
sep=‘‘‘,.?!":‘‘‘
exclude={‘the‘,‘and‘,‘of‘,‘to‘}

for c in sep:
    news=news.replace(c,‘ ‘)
wordList=news.lower().split()

wordDict={}

wordSet=set(wordList)-exclude
for w in wordSet:
    wordDict[w]=wordList.count(w)

dictList=list(wordDict.items())
dictList.sort(key=lambda x:x[1],reverse=True)

f=open(‘newscount.txt‘,‘a‘)
for i in range(25):
    f.write(dictList[i][0]+‘ ‘+str(dictList[i][1])+‘\n‘)
f.close()

  技术分享图片

       技术分享图片

 

综合练习:词频统计

原文:https://www.cnblogs.com/Lorz/p/8658715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!