综合练习:词频统计

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])+'
')
f.close()

  

       

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