1.英文
import string
fo=open(‘song.txt‘,‘r‘,encoding="utf-8") #导入
song =fo.read().lower() # 全部变成小写
fo.close()
print(song)
sep=‘‘‘ .!;?-_:‘‘‘
for quchu in sep:
song =song.replace(quchu,‘ ‘)
strlist = song.split()
print(len(strlist),strlist)
strset = set(strlist)
noMean ={‘i‘,‘are‘,‘for‘,‘a‘,‘the‘,"i‘ve",‘on‘,‘of‘,‘at‘,‘if‘,"i‘d",‘to‘,‘it‘,‘do‘,‘not‘,‘wouldn t‘,‘what‘,‘up‘,‘or‘,‘but‘,‘this‘,‘one s‘,‘could‘,‘and‘}
strset =strset-noMean
print(len(strset),strset)
strdist = {}
for word in strset:
strdist[word] = strlist.count(word)
print(len(strdist),strdist)
for word in strset:
strdist[word] = strlist.count(word)
print(len(strdist),strdist)
wordlist = list(strdist.items())
wordlist.sort(key=lambda x:x[1],reverse=True)
print(strlist)
for i in range(20):
print(wordlist[i])
原文:https://www.cnblogs.com/czx98/p/9790099.html