码迷,mamicode.com
首页 > 其他好文 > 详细

综合练习:词频统计

时间:2018-03-26 12:37:25      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:lambda   for   rev   div   log   sort   img   nis   list   

f=open(‘/Users/Administrator/Desktop/we.txt‘,‘r‘)
text=f.read()
f.close()
print(text)

rep = ‘‘‘,.!:?‘‘‘
exclude={‘a‘,‘the‘,‘for‘,‘in‘,‘i‘,‘my‘,‘that‘,‘of‘,‘have‘,‘and‘,‘had‘,‘it‘,‘are‘}
for c in rep:
     str=text.replace(c,"")
str=str.lower().split()
for i in str:
     print(i)

dict01={}
wordset=set(str)-exclude
for w in wordset:
    dict01[w]=str.count(w)
# set01 = set(str)
# print(set01)
# for item in set01:
#     dict01.update({item:str.count(item)})
# print(dict01)
for c in str:
    dict01[c]=dict01.get(c,0)+1
for w in exclude:
    dict01.pop(w)
dictlist=dict01.items()
dictlist=sorted(dict01.items(),key=lambda d:d[1],reverse=True)
for c in range(10):
    print(dictlist[c])

技术分享图片

 技术分享图片

 

技术分享图片

 

综合练习:词频统计

标签:lambda   for   rev   div   log   sort   img   nis   list   

原文地址:https://www.cnblogs.com/qq157049540/p/8649757.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!