Collections 常用工具

简单介绍defaultdict、setdefault、namedtuple、deque、Counter、OrderedDict的使用

defaultdict

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
例1:
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(list)
for x,y in s:
d[x].append(y)

结果 defaultdict(list, {'blue': [2, 4], 'red': [1], 'yellow': [1, 3]})

例2:
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
d = defaultdict(int)
for x,y in s:
d[x]+=y

结果 defaultdict(int, {'blue': 6, 'red': 1, 'yellow': 4})

setdefault

1
2
3
4
5
6
7
8
9
10
11
例1:
dict = {'runoob': '菜鸟教程', 'google': 'Google 搜索'}
print "Value : %s" % dict.setdefault('runoob', None)
print "Value : %s" % dict.setdefault('Taobao', '淘宝')

例2:
d = {}
s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
for x,y in s:
d.setdefault(x,[]).append(y)
结果 {'blue': [2, 4], 'red': [1], 'yellow': [1, 3]}

namedtuple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
s = [(1, 'Wu', 90), (2, 'Xing', 89), (3, 'Yuan', 98), (4, 'Wang', 95)]
stu = namedtuple("s",["id","name","score"]) # s是名字,一般和前面变量名一样,也可以不同
for x in s:
s1 = stu._make(x)
print s1
print s1.name

结果
s(id=1, name='Wu', score=90)
s(id=2, name='Xing', score=89)
s(id=3, name='Yuan', score=98)
s(id=4, name='Wang', score=95)
Wu
Xing
Yuan
Wang

deque

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
deque这个对象类似于list列表,不过你可以操作它的“两端,list也能实现操作两端的数据(insert,pop),但是deque是线程安全的,且list对象的这两种用法的时间复杂度是 O(n) ,也就是说随着元素数量的增加耗时呈 线性上升。而使用deque对象则是 O(1) 的复杂度。

例1:
import collections
d=collections.deque('abcdefg')
print 'Deque:',d
print 'Length:',len(d)
print 'Left end:',d[0]
print 'Right end:',d[-1]

d.remove('c')
print 'remove(c):',d

结果
Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
Length: 7
Left end: a
Right end: g
remove(c): deque(['a', 'b', 'd', 'e', 'f', 'g'])

例2:
import collections
d1=collections.deque()
d1.extend('abcdefg')
print 'extend:',d1
d1.append('h')
print 'append:',d1
# add to left
d2=collections.deque()
d2.extendleft(xrange(6))
print 'extendleft:',d2
d2.appendleft(6)
print 'appendleft:',d2

结果
extend: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g'])
append: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'])
extendleft: deque([5, 4, 3, 2, 1, 0])
appendleft: deque([6, 5, 4, 3, 2, 1, 0])

例3:还可以使用rotate做出循环跑马灯的效果
a = deque('>--------------------')

while True:
a.rotate(1)
print '\r%s' % ''.join(a)
time.sleep(2)

结果
->-------------------
-->------------------
--->-----------------

Counter

1
2
3
4
5
6
7
8
9
10
from collections import Counter

s = '''A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts. The Counter class is similar to bags or multisets in other languages.'''.lower()

c = Counter(s)
# 获取出现频率最高的5个字符
print c.most_common(5)

结果:
[(' ', 54), ('e', 32), ('s', 25), ('a', 24), ('t', 24)]

OrderedDict

在Python中,dict这个数据结构由于hash的特性,是无序的,这在有的时候会给我们带来一些麻烦, 幸运的是,collections模块为我们提供了OrderedDict,当你要获得一个有序的字典对象时,用它就对了。

1
2
3
4
5
6
7
8
9
10
11
12
from collections import OrderedDict
items = (('A', 1), ('B', 2), ('C', 3))
from collections import OrderedDict

a = dict(items)
print a
b = OrderedDict(items)
print b

结果
{'A': 1, 'C': 3, 'B': 2}
OrderedDict([('A', 1), ('B', 2), ('C', 3)])