统计一个文件中某个单词的出现次数 本文最后更新于 2016年11月30日 下午 首先有一个最简单的方法,就是用正则匹配 123import retmp = open("123.txt", "r").read()print len(re.findall("hello", tmp)) 还可以利用Counter来实现更多的功能 1234567891011121314import collectionsimport re pa = re.compile("\w+")counter = collections.Counter(patt.findall(open('reparser.py','rt').read())) # top 100for word, times in counter.most_common(100): print word, times # find wordcounter_dict = dict(counter.most_common(0))tobefind = 'hello'print tobefind, counter_dict.get(tobefind, 0) Python 统计一个文件中某个单词的出现次数 https://yuluod.github.io/2016/11/30/统计一个文件中某个单词的出现次数/ 作者 yuluo 发布于 2016年11月30日 许可协议 Linux防火墙的关闭 上一篇 把SQL的显示结果输出到文本中 下一篇