Python容器数据类型——collections
2012-08-06 13:58
369 查看
#!/usr/bin/python #coding=utf-8 #http://docs.python.org/library/collections.html #count对象 Only 2.7 from collections import Counter #统计字母出现的次数 Counter('hello world') Counter(['red', 'blue', 'red', 'green', 'blue', 'blue']) #小于等于0的会被忽略 c = Counter(a=4, b=2, c=0, d=-2) list(c.elements()) #取前三个最多的字母 Counter('hello world').most_common(3) #堆 from collections import deque d = deque('abc') d.append('d') d.pop() #后入先出 d.popleft() #先入先出 #返回最后n行文本 deque(open(filename), n) #defaultdict from collections import defaultdict #使用list初始化一个dict d = defaultdict(list) d["yellow"].append(1) d["red"].append(2) d["yellow"].append(3) print d.items() #[('red', [2]), ('yellow', [1, 3])] #用int初始华一个dict d = defaultdict(int) d["yellow"] += 1 d["red"] += 2 d["yellow"] += 3 print d.items() #[('red', 2), ('yellow', 4)]
相关文章推荐
- [Python标准库]collections——容器数据类型[一]
- [Python标准库]collections——容器数据类型[二]
- python之Collections容器数据类型
- collections——高性能容器数据类型
- 【Python】 更多数据类型collections&简易数据文件shelve
- python基础数据类型及内置容器详解
- Python容器的数据类型 (python2)
- Python高级数据类型模块collections
- collections 容器数据类型
- Python数据类型详解(一)字符串
- 菜鸟学习Python之数据类型
- 走进Python世界(五)数据类型 1.数字(Number)
- php redis mongo python 数据类型小结
- Python基本内置数据类型有哪些?
- python变量和数据类型
- Python基本数据类型
- Python 核心编程笔记_Chapter_5_Note_1 数据类型及相关运算
- python标准数据类型
- Python数据类型 -- 元组合集合
- Python基础语法之内置的数据类型list和tuple