Python 练习册 => 0004 任一个英文的纯文本文件,统计其中的单词出现的个数
- Python
- 2017-11-23
- 159热度
- 0评论
心得
- python 使用正则表达式进行文本匹配,参考
- 使用 collections.Counter 统计频率
- 使用 lambda 函数排序
文本(zen_of_python.txt)内容为:
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
代码:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import pprint
from collections import Counter
'''
1. 缩写单独视为一个单词,例如 You're 和 you,are 是不同的单词;I'm 与 I,am 是不同的单词。
2. 统一转换为小写
'''
def main():
with open('zen_of_python.txt') as f:
origin_text = f.read()
wordpat = re.compile(r"([a-zA-Z]+([-|'][a-zA-Z]+)?)")
wordlist = [item[0].lower() for item in wordpat.findall(origin_text)]
wordcount = Counter(wordlist)
result = [(k, v) for k, v in wordcount.items()]
result.sort(key=lambda x: x[1], reverse=True)
pprint.pprint(result)
if __name__ == "__main__":
main()
运行结果
[('is', 10),
('better', 8),
('than', 8),
('the', 6),
('to', 5),
('of', 3),
('although', 3),
('never', 3),
('be', 3),
('one', 3),
('idea', 3),
('complex', 2),
('special', 2),
('should', 2),
('unless', 2),
('obvious', 2),
('way', 2),
('do', 2),
('it', 2),
('may', 2),
('now', 2),
('if', 2),
('implementation', 2),
('explain', 2),
('a', 2),
('zen', 1),
('python', 1),
('by', 1),
('tim', 1),
('peters', 1),
('beautiful', 1),
('ugly', 1),
('explicit', 1),
('implicit', 1),
('simple', 1),
('complicated', 1),
('flat', 1),
('nested', 1),
('sparse', 1),
('dense', 1),
('readability', 1),
('counts', 1),
('cases', 1),
("aren't", 1),
('enough', 1),
('break', 1),
('rules', 1),
('practicality', 1),
('beats', 1),
('purity', 1),
('errors', 1),
('pass', 1),
('silently', 1),
('explicitly', 1),
('silenced', 1),
('in', 1),
('face', 1),
('ambiguity', 1),
('refuse', 1),
('temptation', 1),
('guess', 1),
('there', 1),
('and', 1),
('preferably', 1),
('only', 1),
('that', 1),
('not', 1),
('at', 1),
('first', 1),
("you're", 1),
('dutch', 1),
('often', 1),
('right', 1),
('hard', 1),
("it's", 1),
('bad', 1),
('easy', 1),
('good', 1),
('namespaces', 1),
('are', 1),
('honking', 1),
('great', 1),
("let's", 1),
('more', 1),
('those', 1)]