当前位置: 移动技术网 > IT编程>脚本编程>Python > 统计文件中单词出现频率最高的10个以及他们出现的次数

统计文件中单词出现频率最高的10个以及他们出现的次数

2020年01月17日  | 移动技术网IT编程  | 我要评论

qq主人寄语,爱love 下载,音响图片

import re

regex = "[a-za-z]+"

with open("./test.py") as f:
    lines = f.readlines()

worddict = dict()
for line in lines:
    words = re.findall(regex, line)
    for word in words:
        if word in worddict.keys():
            worddict[word] += 1
        else:
            worddict[word] = 1

words_top10 = sorted(worddict.items(), key=lambda x: x[1], reverse=true)

print(words_top10) 

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网