This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
forked from jbub/spy-data-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolls.py
69 lines (52 loc) · 1.46 KB
/
colls.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
import collections
#
# namedtuple
#
Rectangle = collections.namedtuple('Rectangle', ['a', 'b'], verbose=True)
my_rect = Rectangle(a=10, b=20)
my_rect # >>> Rectangle(a=10, b=20)
a, b = my_rect
a, b # >>> (10, 20)
#
# deque
#
d = collections.deque('hello')
d.append('x') # add new item to right side
d.appendleft('y') # add new item to left side
d # >>> deque(['y', 'h', 'e', 'l', 'l', 'o', 'x'])
d.pop() # pop item from right side
d.popleft() # pop item from left side
d # >>> deque(['h', 'e', 'l', 'l', 'o'])
d.extend([4, 5, 6]) # add new item to right side
d.extendleft([1, 2, 3]) # add new item to left side
d # >>> deque([3, 2, 1, 'h', 'e', 'l', 'l', 'o', 4, 5, 6])
#
# Counter
#
counter = collections.Counter('collections')
counter['c'] # >>> 2
counter.most_common(4) # >>> [('l', 2), ('o', 2), ('c', 2), ('s', 1)]
list(counter.elements()) # >>> ['s', 'l', 'l', 'o', 'o', 'c', 'c', 'i', 'n', 'e', 't']
#
# OrderedDict
#
my_dict = collections.OrderedDict()
my_dict['key'] = 2
my_dict['key_two'] = 3
my_dict['key_three'] = 4
my_dict # >>> OrderedDict([('key', 2), ('key_two', 3), ('key_three', 4)])
#
# defaultdict
#
# group a sequence of key-value pairs into a dictionary of sets
data = [
('add', 2),
('delete', 3),
('add', 4),
('edit', 10),
('edit', 6),
]
things = collections.defaultdict(set)
for action, number in data:
things[action].add(number)
things # >>> defaultdict(<class 'set'>, {'edit': set([10, 6]), 'add': set([2, 4]), 'delete': set([3])})