统计文件中单词出现频率最高的10个以及他们出现的次数
程序员文章站
2022-05-28 23:09:53
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) ... ......
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)
下一篇: JS高级---正则表达式其他方法的使用