统计Hamlet文本文件中所有的的单词词频,并把词频排名前100的单词和出现频次保存到一个文本文件中,并将文件名统一格式为“姓名.txt”
def getText():
txt = open("C:/Users/Lenovo/Desktop/hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch," ")
return txt
hamletText = getText()
words = hamletText.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)
a=sum([len(line.split()) for line in open("C:/Users/Lenovo/Desktop/hamlet.txt", 'r')])
#print(a)
for i in range(a-1):
word,count = items[i]
print("{0:<10}{1:>5}".format(word,count))
有帮助的话点个采纳,谢谢
你可以参考这篇文章看一下,希望对你有帮助python词频统计实例 - 知乎 (zhihu.com)
求完整代码,拜托各位大神了
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632