先下载好hamlet的英文版本,用python统计书中出现次数最高的单词
代码如下:
def getText(): #处理特殊字符,把文章单词全部变成小写
txt = open("Hamlet.txt","r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch," ")
return txt
hamletTxt = getText() #调用定义的函数
words = hamletTxt.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(10):
word,count = items[i]
print("{0:<10}{1:>5}".format(word,count))
图片如下: