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

综合练习:词频统计

时间:2018-03-26 12:28:43      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:count   item   str   ict   clu   ems   image   nbsp   bsp   

text= open("aaa.txt","r")
ls=text.read()
text.close()
print(ls)
str = ‘‘‘,.‘"!?:‘‘‘
enclude={‘for‘,‘you‘,‘a‘,‘and‘,‘the‘,}
for i in str:
    ls = ls.replace(i," ")
    # print(ls)
ls=ls.lower().split()
print(ls)
lsDict={}
lsset = set(ls)-enclude
for i in lsset:
    lsDict[i]=ls.count(i)
for w in lsDict:
    print(w,lsDict[w])

# for i in ls:
#     lsDict[i] = lsDict.get(i,0)+1
# for w in lsDict:
#     print(w,lsDict[w])

dictlist = list(lsDict.items())
dictlist.sort(key=lambda x:x[1],reverse=True)
for i in dictlist:
    print(i)

for i in range(20):
    print(dictlist[i])

技术分享图片

 


  

综合练习:词频统计

标签:count   item   str   ict   clu   ems   image   nbsp   bsp   

原文地址:https://www.cnblogs.com/mimimi/p/8649849.html

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