python模块-collections
2018-02-28 09:50
459 查看
一、Counter (主要用来做统计)
from collections import Counter c = Counter('abcdeabcdabcaba') print c.most_common(3) # [('a', 5), ('b', 4), ('c', 3)] print sorted(c.elements()) # ['a', 'a', 'a', 'a', 'a', 'b', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e']
二、defaultdict
from collections import defaultdict def a(): return [1,2,3] # 第一个元素必须为callable,list set 等 dd = defaultdict(a, a='b') # dd是一个字典,他的值是list类型 print dd['a'] # 如果存在键,则打印出所对饮的值 b print dd['b'] # 如果不存在键,则打印出a中的值。 [1,2,3]
三、OrderedDict
# 他就是初始化了一个有序的字典 d = OrderedDict() d['a'] = 'A' d['b'] = 'B' d['c'] = 'C' for k, v in d.items(): print k, v
四、deque
from collections import deque # 双端序列,和列表操作差不多。详见collections模块 q = deque('12345') print q # deque(['1', '2', '3', '4', '5'])
相关文章推荐
- Python的collections模块中defaultdict类型的用法
- python常用模块——collections
- Python全栈开发之5、几种常见的排序算法以及collections模块提供的数据结构
- 【python】collections模块(有序字典,计数器,双向队列)
- Python中Collections模块的Counter容器类使用教程
- Python 模块collections
- 简介Python的collections模块中defaultdict类型的用法
- python基础之collections模块
- python模块介绍- collections(5)-OrderedDict 有序字典
- python的Collections 模块
- Python的collections模块中的OrderedDict有序字典
- Python的collections模块中的OrderedDict有序字典
- Python:collections模块的用法
- Python系列之Collections内置模块(2)
- Python collections模块总结
- python模块之collections
- python中collections 模块里的defaultdict()函数
- python 内置模块:collections
- Python的collections模块中namedtuple结构使用示例
- python模块学习之数据结构--collections.counter