综合练习:词频统计
Posted 099吴海经
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了综合练习:词频统计相关的知识,希望对你有一定的参考价值。
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])
以上是关于综合练习:词频统计的主要内容,如果未能解决你的问题,请参考以下文章