-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionaries.py
126 lines (90 loc) · 2.14 KB
/
dictionaries.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
ages = {
'Mary': 31,
'Jonathan': 28,
'Dick': 51
}
# Bad
if 'Dick' in ages:
age = ages['Dick']
else:
age = 'Unknown'
# Better, specifies parameter to default to
age = ages.get('Dick', 'Unknown')
import string
cities = [
'Groningen',
'Marseille',
'Buenos Aires',
'Mumbai'
]
populations = [
197823,
852516,
2890151,
12442373
]
# Bad
d = {}
for city, population in zip(cities, populations):
d[string.capwords(city)] = population
print(d)
# Better, dictionary comprehension
d = { string.capwords(city) : population for city, population in zip(cities, populations) }
print(d)
# Better, preserves order
import collections
d = collections.OrderedDict()
for city, population in zip(cities, populations):
d[string.capwords(city)] = population
print(d)
import collections
butterfly_observations = {
'Brown Clipper': 2,
'Common Mormon': 11,
'Giant Atlas Moth': 1,
'Blue Peacock': 3
}
# Bad
if 'Palmfly' in butterfly_observations:
palmfly_observations = butterfly_observations['Palmfly']
else:
palmfly_observations = 0
# Better, specifies default value in case of misses
palmfly_observations = butterfly_observations.get('Palmfly', 0)
# Better, a proper dictionary structure with a default action in case of misses
d = collections.defaultdict(lambda: 0)
d.update(butterfly_observations)
palmfly_observations = d['Palmfly']
# Even better in this scenario where the dictionary stores quantitative values
butterfly_counter = collections.Counter(butterfly_observations)
palmfly_observations = butterfly_counter['Palmfly']
colors = [ 'Red', 'Green', 'Red', 'Blue', 'Green', 'Red' ]
# Ok
d = {}
for color in colors:
if color not in d:
d[color] = 0
d[color] += 1
# Better
d = {}
for color in colors:
d[color] = d.get(color, 0) + 1
# Better
import collections
d = collections.defaultdict(int)
for color in colors:
d[color] += 1
names = [ 'Raymond', 'Rachel', 'Matthew', 'Roger', 'Betty', 'Melissa', 'Judith', 'Charlie' ]
# Bad
d = {}
for name in names:
key = len(name)
if key not in d:
d[key] = []
d[key].append(name)
# Better
import collections
d = collections.defaultdict(list)
for name in names:
key = len(name)
d[key].append(name)