-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15-python-collections-module.py
76 lines (59 loc) · 1.62 KB
/
15-python-collections-module.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
from collections import Counter
from collections import defaultdict
from collections import OrderedDict
from collections import namedtuple
list1 = [1, 2, 3, 4, 5, 6, 1, 2, 3, 3]
print(Counter(list1))
word = 'word'
print(Counter(word))
animals = "cat dog bird dog bird cat"
print(Counter(animals))
counter = Counter(animals.split())
print(counter.most_common(1))
print(counter.most_common(2))
print(counter.most_common())
list1 = [10, 20, 30, 40, 50, 50, 40, 20, 30, 10, 10]
counter = Counter(list1)
print(counter.items())
print(counter.keys())
print(counter.values())
print(sum(counter.values()))
print(list(counter))
dictionary = dict(counter)
print(dictionary)
print(set(counter))
dictionary = defaultdict(int)
print(dictionary['some'])
print(dictionary)
dictionary = defaultdict(str)
print(dictionary['some'])
print(dictionary)
dictionary = defaultdict(object)
print(dictionary['some'])
print(dictionary)
dictionary = defaultdict(int)
dictionary['some'] = 10.5
print(dictionary['something'])
print(dictionary)
dictionary = {'name': 'Jonh', 'last_name': 'Doe'}
print(dictionary)
dictionary = OrderedDict()
dictionary['name'] = 'Jonh'
dictionary['last_name'] = 'Doe'
print(dictionary)
dictionary1 = {'name': 'Jonh', 'last_name': 'Doe'}
print(dictionary1)
dictionary2 = {'last_name': 'Doe', 'name': 'Jonh'}
print(dictionary2)
print(dictionary1 == dictionary2)
tuple1 = (10, 20, 30, 40)
print(tuple1[0])
print(tuple1[-1])
Person = namedtuple('Person', 'name last_name age')
person = Person(name='Jonh', last_name='Doe', age=28)
print(person.name)
print(person.last_name)
print(person.age)
print(person[0])
print(person[1])
print(person[-1])