原码
def getText():
txt=open("hmlt.txt","r").read()
txt=txt.lower()
for ch in '`!@#~$%^&*()_+-=*/{}[];,./?<>':
txt=txt.replace(ch," ")
return txt
hmltTxt=getText()
words=hmltTxt.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(100):
word,count=items[i]
print("{0:<10}{1:>5}".format(word,count))
带解析
def getText():
txt=open("hmlt.txt","r").read() #打开文件
txt=txt.lower() #将所有单词转换为小写去掉大小写的干扰
for ch in '`!@#~$%^&*()_+-=*/{}[];,./?<>': #去掉所有的特殊符号
txt=txt.replace(ch," ") #将特殊符号替换成空格 即去掉
return txt
hm