Hamlet词频统计

def getText():
txt=open(“hamlet.txt”,“r”).read()
txt=txt.lower() #将所有字母转换成小写
for ch in ‘|"#$%&()*+,-./:;<=>?@[\]^{|}~’:
txt=txt.replace(ch," “) #完成对txt的遍历,用空格代替特殊符号
return txthamlet
Txt=getText()
words=hamletTxt.split() #分隔字符,默认分割字符为空格
counts = {}#定义一个空字典
for word in words:
counts[word] = counts.get(word,0) + 1 #若在遍历中找到单词,则返回该单词+1后的个数,若该单词不在,则将这个单词加到字典中,count+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))
源代码出自MOOC Python程序语言设计6.6添加链接描述
关于sort()的详细解释在https://blog.csdn.net/weixin_38198369/article/details/105021708

你可能感兴趣的:(小结,python)