-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollectionInPython.py
58 lines (48 loc) · 965 Bytes
/
CollectionInPython.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
# collections : Counter
from collections import Counter
a = "aaaaaabbcccc"
my_counter = Counter(a)
print(my_counter)
print(my_counter.values())
print(my_counter.most_common(1))
print(my_counter.most_common(2)[0][0])
# different elements
print(list(my_counter.elements()))
# ------
from collections import namedtuple
# create a class like structure
point = namedtuple('Point', 'x, y')
pt = point(1, -4)
print(pt)
print(pt.x, pt.y)
# ---
# remeber the order the element added
from collections import OrderedDict
ordered_dict = OrderedDict()
ordered_dict['a'] = 1
ordered_dict['b'] = 1
ordered_dict['c'] = 1
ordered_dict['d'] = 1
ordered_dict['e'] = 1
print(ordered_dict)
# ---
from collections import defaultdict
d = defaultdict(list)
d['a'] = 1
d['b'] = 2
print(d['c'])
# ---
from collections import deque
d = deque()
d.append(1)
d.append(2)
d.appendleft(3)
print(d)
d.pop()
print(d)
d.popleft()
print(d)
d.extendleft([4,5,6])
print(d)
d.rotate(1)
print(d)