def getText():
txt=open(r'C:\Users\jxiong\Desktop\xu\1.txt','r',encoding='utf-8').read()
txt=txt.lower()
for ch in "~@#$%^&*()_-+=<>?/,.:;{}[]|\'""":
txt=txt.replace(ch,' ')
return txt
hamletTxt=getText()
words=hamletTxt.split()
counts={}
sumcount = 0
for word in words:
counts[word]=counts.get(word,0)+1
sumcount = sumcount + 1
sorted_word_freq = sorted(counts.items(), key=lambda v: v[1], reverse=True)
for item in sorted_word_freq[:5]: # 输出 Top 5 的单词
print(item[0], item[1])
重点就是字典的排序。