统计Hamlet文本文件中所有的的单词词频,并把词频排名前100的单词和出现频次保存到一个文本文件中,并将文件名统一格式为“姓名.txt”
4条回答 默认 最新
- CSDN专家-深度学习进阶 2021-05-16 10:56关注
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))
有帮助的话点个采纳,谢谢
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 1无用
悬赏问题
- ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
- ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
- ¥15 cmd cl 0x000007b
- ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
- ¥500 火焰左右视图、视差(基于双目相机)
- ¥100 set_link_state
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号