综合练习:词频统计
fo =open("jj.txt","r") news=fo.read() fo.close sep='''",.!?:''' exclude={'the','she','you','it','a','and','is','in','so'} for i in sep: news= news.replace(i," ") wordList=news.lower().split() wordDict={} wordSet= set(wordList)-exclude for c in wordSet: wordDict[c]=wordList.count(c) '''for c in wordDict: print(c,wordDict[c])''' dictList= list(wordDict.items()) dictList.sort(key=lambda x:x[1],reverse=True) for w in range(20): print(dictList[w])