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

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) 

 

posted @ 2020-01-17 10:36  琵琶真的行  阅读(982)  评论(0编辑  收藏  举报