python collections模块/系列

python collections系列

http://www.cnblogs.com/wupeiqi/articles/5115190.html

http://www.zlovezl.cn/articles/collections-in-python/

http://docs.python.org/2/library/collections.html

 

我们都知道,Python拥有一些内置的数据类型,比如str, int, list, tuple, dict等, collections模块在这些内置数据类型的基础上,提供了几个额外的数据类型:

  • namedtuple(): 生成可以使用名字来访问元素内容的tuple子类
  • deque: 双端队列,可以快速的从另外一侧追加和推出对象
  • Counter: 计数器,主要用来计数主要是对字典操作。
  • OrderedDict: 有序字典
  • defaultdict: 带有默认值的字典

counter计数器

Counter是对字典类型的补充,用于追踪值的出现次数。

具备字典的所有功能 + 自己的功能

>>> import  collections
>>> s = '111112222223333333444'
>>> collections.Counter('1')
Counter({'1': 1})
>>> collections.Counter()   
Counter()
>>> collections.Counter(s)
Counter({'3': 7, '2': 6, '1': 5, '4': 3})
most_common 出现最多次
>>> q = collections.Counter(s)
>>> q.most_common(2)         				### most_common出现最多次
[('3', 7), ('2', 6)]
>>>		
elements 元素
>>> for i in q.elements():							### 元素
...     print(i)
... 
1
1
1
1
1
4
4
4
3
3
3
3
3
3
3
2
2
2
2
2
2
>>>  
>>> q
Counter({'3': 7, '2': 6, '1': 5, '4': 3})
>>> s
'111112222223333333444'
>>> for i in q.items():
...     print(i)
... 
('1', 5)
('4', 3)
('3', 7)
('2', 6)
>>> for k,v in q.items(): 
...     print(k,v)
... 
1 5
4 3
3 7
2 6
>>>
update subtract 增加减少
>>> s
'111112222223333333444'
>>> q = collections.Counter(s)                    
>>> q
Counter({'3': 7, '2': 6, '1': 5, '4': 3})
>>> q.update(['3','9','11','11'])
>>> q
Counter({'3': 8, '2': 6, '1': 5, '4': 3, '11': 2, '9': 1})
>>> q.s
q.setdefault(  q.subtract(    
>>> q.s
q.setdefault(  q.subtract(    
>>> q.subtract(['3','0'])
>>> q
Counter({'3': 7, '2': 6, '1': 5, '4': 3, '11': 2, '9': 1, '0': -1})
>>>

OrderedDict:有序字典

orderdDict是对字典类型的补充,他记住了字典元素添加的顺序。OrderedDict的Key会按照插入的顺序排列,不是Key本身排序:

>>> from collections import OrderedDict
>>> d = dict([('a', 1), ('b', 2), ('c', 3)])
>>> d                               # dict的key是无序的
{'b': 2, 'c': 3, 'a': 1}
>>> od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>> od
OrderedDict([('a', 1), ('b', 2), ('c', 3)])
>>>


>>> import collections
>>> d = collections.OrderedDict()
>>> d['k1'] = 'v1'   
>>> d['k2'] = 'v2' 
>>> d['k3'] = 'v3' 
>>> d
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> d
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')])
>>> 
>>> 
>>> 
>>> d2 = dict()
>>> d2['k1'] = 'v1'
>>> d2['k2'] = 'v2' 
>>> d2['k3'] = 'v3' 
>>> d2
{'k3': 'v3', 'k2': 'v2', 'k1': 'v1'}
popitem  取最后并删除
>>> import collections
>>> d = collections.OrderedDict()
>>> d['k1'] = 'v1'  
>>> d['k2'] = 'v2' 
>>> d['k3'] = 'v3' 
>>> 
>>> ret = d.popitem()
>>> print(ret)
('k3', 'v3')
>>> ret = d.popitem()
>>> print(ret)       
('k2', 'v2')
>>>
>>> d
OrderedDict([('k1', 'v1')])
>>>
setdefault 设置默认值
>>> d
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', None)])
>>> d.setdefault('k4','v4')
>>> d
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', None)])
>>>
update更新
>>> d
OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3'), ('k4', None)])
>>> d.update({'k1':'v11','k10':'v10'})
>>> d
OrderedDict([('k1', 'v11'), ('k2', 'v2'), ('k3', 'v3'), ('k4', None), ('k10', 'v10')])
>>>

defaultdic默认字典

>>> dic = {}
>>> dic['k1'].append('v1')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'k1'
>>> import collections                  
>>> dic2 = collections.defaultdict(list)
>>> dic2['k2'].append('v2')
>>> dic
{}
>>> dic2
defaultdict(<class 'list'>, {'k2': ['v2']})
>>> 
>>> type(dic)
<class 'dict'>
>>> type(dic2)
<class 'collections.defaultdict'>
>>>



1 #有如下值集合,不用默认字典执行代码如下
 2 #[11,22,33,44,111,222]  
 3 #将所有大于30的值保存在字典的第一个key中,
 4 #将小于等于30的值保存在第二个key的值(列表)中。
 5 #即:{"k1":大于66,"k2":小于等于66}
>>> v = [11,22,33,44,111,222]  
>>> dic = {}
>>> for i in v:
...     if i > 30:
...             if 'k1' in dic.keys():
...                     dic['k1'].append(i)
...             else:
...                     dic['k1'] = [i]
...     else:
...             if 'k2' in dic.keys():
...                     dic['k2'].append(i)
...             else:
...                     dic['k2'] = [i]
... 
>>> dic
{'k1': [33, 44, 111, 222], 'k2': [11, 22]}
>>>  
>>> import collections
>>> v = [11,22,33,44,111,222]              
>>> dic2 = collections.defaultdict(list)
>>> for i in v:
...     if i>30:
...             dic2['k1'].append(i)
...     else:
...             dic2['k2'].append(i)
... 
>>> dic2
defaultdict(<class 'list'>, {'k1': [33, 44, 111, 222], 'k2': [11, 22]})
>>>

namedtuple  可命名元祖tuple子类

namedtuple主要用来产生可以使用名称来访问元素的数据对象,通常用来增强代码的可读性, 在访问一些tuple类型的数据时尤其好用。

>>> import collections
>>> Mytuple = collections.namedtuple('Mytuple',['x', 'y', 'z'])
>>> Mytuple
<class '__main__.Mytuple'>
>>> c = Mytuple(11,22,33)
>>> c
Mytuple(x=11, y=22, z=33)
>>>  c.
c.count(  c.index(  c.x       c.y       c.z       
>>>  c.x
  File "<stdin>", line 1
    c.x
    ^
IndentationError: unexpected indent
>>> c.x 
11
>>> c.y
22
>>> c.z
33
>>>


point = [
    ('wangxu','100','perfect'),
    ('zhangsan','80','good'),
    ('lisi','30','oh,no!!!')
]
import collections
re = collections.namedtuple('分数',['name','point','status']) 
for i in point:  
	i = re._make(i)
	print(i)

... 
分数(name='wangxu', point='100', status='perfect')
分数(name='zhangsan', point='80', status='good')
分数(name='lisi', point='30', status='oh,no!!!')
>>>

deque: 双向队列

deque其实是 double-ended queue 的缩写,翻译过来就是双端队列,它最大的好处就是实现了从队列 头部快速增加和取出对象: .popleft(), .appendleft() 。

import sys
import time
from collections import deque

fancy_loading = deque('>--------------------')

while True:
    print ('\r%s' % ''.join(fancy_loading)),
    fancy_loading.rotate(1)
    sys.stdout.flush()
    time.sleep(0.08)

# 一个无尽循环的跑马灯
------------->-------
--->-----------------
(None,)

 

>>> import collections
>>> d = collections.deque('1')
>>> d
deque(['1'])
>>> d.append('2')
>>> d
deque(['1', '2'])
>>> d.appendleft('111')
>>> d
deque(['111', '1', '2'])
>>> d.count('2')
1
>>> d.count('1')
1
>>> d.appendleft('1')  
>>> d
deque(['1', '111', '1', '2'])
>>> d.count('1')     
2
>>>
>>> d.extend('1,2,3')
>>> d
deque(['1', '111', '1', '2', '1', ',', '2', ',', '3'])
>>> d.extend(['22','33'])
>>> d
deque(['1', '111', '1', '2', '1', ',', '2', ',', '3', '22', '33'])
>>> d.extendleft(['22','33']) 
>>> d
deque(['33', '22', '1', '111', '1', '2', '1', ',', '2', ',', '3', '22', '33'])
>>> d.index('33')
0
>>> d.index('22')
1
>>> d.pop()
'33'
>>> d
deque(['33', '22', '1', '111', '1', '2', '1', ',', '2', ',', '3', '22'])
>>> d.popleft()
'33'
>>> d
deque(['22', '1', '111', '1', '2', '1', ',', '2', ',', '3', '22'])
>>> d.insert('777',0)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object cannot be interpreted as an integer
>>> d.insert(0,'777')
>>> d
deque(['777', '22', '1', '111', '1', '2', '1', ',', '2', ',', '3', '22'])
>>>
deque(['22', '3', ',', '2', ',', '1', '2', '1', '111', '1', '22', '777'])
>>> d.reverse()
>>> d
deque(['777', '22', '1', '111', '1', '2', '1', ',', '2', ',', '3', '22'])
>>>
>>> d.rotate()
>>> d
deque(['22', '777', '22', '1', '111', '1', '2', '1', ',', '2', ',', '3'])
>>> d.rotate()
>>> d
deque(['3', '22', '777', '22', '1', '111', '1', '2', '1', ',', '2', ','])
>>>

 

posted on 2017-07-24 17:10  光阴8023  阅读(199)  评论(0编辑  收藏  举报