您当前的位置: 首页 > 

风间琉璃•

暂无认证

  • 1浏览

    0关注

    337博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

文件词频统计--Hamlet

风间琉璃• 发布时间:2021-11-03 15:27:11 ,浏览量:1

项目场景: Python练习题 解决方案:

提示:这里填写该问题的具体解决方案:

def gettest():
    txt = open("hamlet.txt", 'r').read()
    txt = txt.lower()
    for ch in '!\"#$%&()*+,-./:;?@[\\]^_‘{|}~':
        txt = txt.replace(ch, ' ')
    return txt

hamelet = gettest()
words = hamelet.split()
count = {}
n = eval(input())
num = 0
for word in words:
    count[word] = count.get(word, 0) + 1
items = list(count.items())
items.sort(key=lambda x: x[1], reverse=True)
for i in range(10):
    num += 1
    word, count = items[i]
    if num > n:
        break
    print("{0:5}".format(word, count))
关注
打赏
1665385461
查看更多评论
立即登录/注册

微信扫码登录

0.0354s