词频统计

fo=open('test.txt','r')
str=fo.read()
fo.close()

rap=''',?!'''
exculde={'the','one','i','to','is','so','and','how','me'}
for c in rap:
    str=str.replace(c," ")
wordList=str.lower().split()

wordDict={}

wordSet=set(wordList)
for c in wordList:
    wordDict[c]=wordList.count(c)


for i in exculde:
    wordDict.pop(i)

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


for i in range(10):
    print(dictList[i])

 

posted on 2018-03-26 11:37  111陈泽翔  阅读(95)  评论(0编辑  收藏  举报