-
Notifications
You must be signed in to change notification settings - Fork 49
/
counter_tutorial.py
51 lines (37 loc) · 988 Bytes
/
counter_tutorial.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
from collections import Counter
def ex1():
c = Counter(['eggs', 'ham', 'eggs'])
print('c[bacon]:', c['bacon'])
print('c[eggs]:', c['eggs'])
print('c:', c)
def ex2():
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
cnt[word] += 1
print(cnt)
def method_elements():
c = Counter(a=4, b=2, c=0, d=-2)
show = sorted(c.elements())
print(show)
print('c.values():', c.values())
print('sum:', sum(c.values()))
def method_most_common():
show = Counter('abracadabra').most_common(3)
print(show)
def patterns():
c = Counter(a=2, b=-4, c=0)
print('+c:', +c)
print('-c:', -c)
# +c # remove zero and negative counts
def method_subtract():
c = Counter(a=4, b=2, c=0, d=-2)
d = Counter(a=1, b=2, c=3, d=4)
c.subtract(d)
print('c:', c)
if __name__ == "__main__":
ex1()
ex2()
method_elements()
method_most_common()
patterns()
method_subtract()