python 之 Counter ---show me the code 0004
2015-01-05 22:03
537 查看
Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value
__author__ = 'Administrator' # encoding:utf-8 # show me the code 0004 # 任一个英文的纯文本文件,统计其中的单词出现的个数。 # 思路:逐行读取文件,将所有单词存入数组,用 Counter 就可以得到数组内每个单词出现的次数了 # with 语句块,退出语句块时候会自动释放打开的文件资源 from collections import Counter def var_count(file="english.txt"): data = [] with open(file) as handle: for line in handle: words = line.split() data.extend(words) print(Counter(data)) if __name__ == "__main__": var_count()
相关文章推荐
- python show-me-the-code 第0004
- Python Show-Me-the-Code 第 0004 题 统计单词
- Python Show-Me-the-Code 第 0017,0018,0019 题 写XML文件
- Show me the code之Python练习册 Q11~12 关键词过滤
- python show-me-the-code 第0010题
- python之 heapq -- show me the code 0006
- python PIL --show me the code 0000
- Python Show-Me-the-Code 第 0022 题 批量图片处理2
- Python Show-Me-the-Code 第 0014,0015,0016 题 写Excel文件
- Show me the code之Python练习册 Q10 生成验证码
- Python Show-Me-the-Code 第 0000 题 图片右上角加数字
- Python Show-Me-the-Code 第 0010 题 生成验证码图片
- Python Show-Me-the-Code 第 0006 题 最重要的词
- Python Show-Me-the-Code 第 0023 题 Web留言簿应用
- Python Show-Me-the-Code 第 0002题
- Python Show-Me-the-Code 第 0001 题 生成激活码
- Python Show-Me-the-Code 第 0008 题 提取HTML正文内容
- Python Show-Me-the-Code 第 0009 题 提取网页中的超链接
- github/python/ show me the code 25题(一)
- Show me the code之Python练习册 Q4~7