-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse_collections.py
82 lines (61 loc) · 1.76 KB
/
use_collections.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/env python3
# -*- coding:utf-8 -*-
# collections 内建的集合模块
from collections import namedtuple
# namedtuple 自定义tuple对象
Point = namedtuple('Point', ['x', 'y'])
p = Point(1, 2)
print(
p.x, # 根据属性引用
p.y
)
isinstance(p, Point)
isinstance(p, tuple)
# namedtuple('名称', [属性list]):
Circle = namedtuple('Circle', ['x', 'y', 'r'])
c = Circle(1, 2, 3)
# deque 插入和删除操作双向列表
from collections import deque
q = deque(['a', 'b', 'c'])
q.append('x')
q.appendleft('y')
print(q)
# defaultdict
from collections import defaultdict
dd = defaultdict(lambda: 'N/A') # 定义不存在时默认返回值
dd['key1'] = 'abc'
print(dd['key1'], dd['key2'])
# OrderedDict 保持dict的key的顺序
from collections import OrderedDict
d = dict([('a', 1), ('b', 2), ('c', 3), ('d', 4)])
print(d) # d是无序的
od = OrderedDict([('c', 1), ('a', 2), ('b', 3)])
print(od)
# 按插入的顺序排列
od = OrderedDict()
od['z'] = 1
od['y'] = 2
od['x'] = 3
print(list(od.keys()))
# OrderedDict可以实现一个FIFO(先进先出)的dict
class LastUpdatedOrderedDict(OrderedDict):
def __init__(self, capacity):
super(LastUpdatedOrderedDict, self).__init__()
self._capacity = capacity
def __setitem__(self, key, value):
containsKey = 1 if key in self else 0
if len(self) - containsKey >= self._capacity:
last = self.popitem(last=False)
print('remove:', last)
if containsKey:
del self[key]
print('set:', (key, value))
else:
print('add:', (key, value))
OrderedDict.__setitem__(self, key, value)
# Counter
from collections import Counter
p = Counter() # 计数器
for str in 'programming':
p[str]+=1
print(p)