-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDict.get method.py
More file actions
38 lines (31 loc) · 1.02 KB
/
Copy pathDict.get method.py
File metadata and controls
38 lines (31 loc) · 1.02 KB
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
word = 'brontosaurus'
d = dict()
for c in word:
if c not in d:
d[c] = 1
else:
d[c] = d[c] + 1
print(d)
# The result is {'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}
# Dictionaries have a method called get that takes a key and a default value. If the
# key appears in the dictionary, get returns the corresponding value; otherwise it
# returns the default value.
#
# For example:
#
# >>> counts = { 'chuck' : 1 , 'annie' : 42, 'jan': 100}
# >>> print(counts.get('jan', 0))
# 100
# >>> print(counts.get('tim', 0))
# 0
#
# We can use get to write our histogram loop more concisely. Because the get
# method automatically handles the case where a key is not in a dictionary, we can
# reduce four lines down to one and eliminate the if statement.
word = 'brontosaurus'
d = dict()
for c in word:
d[c] = d.get(c, 0) + 1
print(d)
# This is the result :- {'b': 1, 'r': 2, 'o': 2, 'n': 1, 't': 1, 's': 2, 'a': 1, 'u': 2}.
# This is same as using the first code