综合练习:词频统计
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])