-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcollections_examples.py
More file actions
36 lines (28 loc) · 879 Bytes
/
collections_examples.py
File metadata and controls
36 lines (28 loc) · 879 Bytes
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
from collections import Counter
from collections import namedtuple
from collections import defaultdict
my_list = [1, 1, 1, 1, 1, 2, 2, 3, 3, 4, 3, 'a', 'abc', "fish"]
print(Counter(my_list))
sentence = "This is a super cool sentence, how cool is this?"
print(Counter(sentence.split()))
letters = "aaaaaaaabbbbbbbcccccdddddddd"
c = Counter(letters)
print(c.most_common())
print(c.most_common(2))
print(list(c))
d = {'a': 10}
print(d['a'])
# print(d['WRONG_KEY']) # this will throw a key error
d = defaultdict(lambda: 0)
d['correct_key'] = 100
print(d['correct_key'])
print(d['wrong_key']) # this will get the default value instead of throwing a key error
my_tuple = (10,20,30)
print(my_tuple[0])
Dog = namedtuple('Dog', ['age', 'breed', 'name'])
sammy = Dog(age=5, breed='Husky', name='Sam')
print(type(sammy))
print(sammy)
print(sammy[0])
print(sammy[1])
print(sammy[2])