【加急】用Python统计每个单词出现的个数,具体要求如图?

求一串完整代码

#2019/12/26/18:24

def getText():
txt=open(r'D:\第十题.txt').read()
#打开文件
txt=txt.lower()#将字母全部转化为小写
for ch in ',-.()':#去掉特殊符号
txt=txt.replace(ch,"")#将特殊符号替换为空格
return txt
Txt=getText()#读取文件
words=Txt.split()#分隔开
counts={}#创建字典
for word in words:
counts[word]=counts.get(word,0)+1
items=list(counts.items())
items.sort(key=lambda x:x[1],reverse=True)
#从大到小排序
for i in range (20):
print(items[i])
温馨提示:答案为网友推荐,仅供参考