本帖最后由 codegay 于 2017-12-11 00:16 编辑
- # -*- coding: utf-8 -*-
- """
- Created on Sun Dec 10 23:28:33 2017
- @author: codegay
- python3 & 编辑器 == spyder
- """
-
- with open("a.txt") as f:
- txt = f.read()
- #剔除不想统计的字符如回车,括号
- char = list(set([r for r in txt if r not in """\n()()"""]))
- result = {r: txt.count(r) for r in char}
- # 排序
- result = sorted(result.items(), key=lambda d: d[1], reverse=True)
-
- from pprint import pprint
- pprint(result)
-
- """
- [('-', 270),
- ('7', 144),
- ('8', 127),
- ('1', 123),
- ('9', 114),
- ('L', 109),
- ('O', 108),
- ('T', 108),
- ('F', 108),
- ('U', 108),
- ('J', 104),
- ('6', 36),
- ('4', 34),
- ('5', 31),
- ('3', 15),
- ('0', 15),
- ('2', 12),
- ('D', 4),
- ('M', 3),
- ('X', 3),
- (' ', 2),
- ('a', 2),
- ('u', 1),
- ('b', 1),
- ('l', 1),
- ('m', 1)]
- """
- 加了个排序。
复制代码
|