-
Notifications
You must be signed in to change notification settings - Fork 8
/
cheatsheet-dictionaries.py
117 lines (83 loc) · 3.19 KB
/
cheatsheet-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
# ____ _ _ _ _
# | _ \(_) ___| |_(_) ___ _ __ __ _ _ __(_) ___ ___
# | | | | |/ __| __| |/ _ \| '_ \ / _` | '__| |/ _ \/ __|
# | |_| | | (__| |_| | (_) | | | | (_| | | | | __/\__ \
# |____/|_|\___|\__|_|\___/|_| |_|\__,_|_| |_|\___||___/
#
# https://docs.python.org/2/library/stdtypes.html#typesmapping
print '\n-- dictionaries'
d = {'a': 1, 'b': 2}
print d['a'] # if key doesn't exist, raise KeyError exception
del d['a'] # if key doesn't exist, raise KeyError exception
# iterate
d = {'a': 1, 'b': 2}
for key, value in d.items():
print key, ':', value
for key in d:
print key, d[key]
# d.fromkeys(iterable[,value=None]) -> dict: with keys from iterable and all same value
d = d.fromkeys(['a', 'b'], 1)
print d # {'a': 1, 'b': 1}
# d.clear() -> removes all items from d
d = {'a': 1, 'b': 2}
d.clear()
print d # {}
# d.items() -> list: copy of d's list of (key, item) pairs
d = {'a': 1, 'b': 2}
print d.items() # [('a', 1), ('b', 2)]
# d.keys() -> list: copy of d's list of keys
d = {'a': 1, 'b': 2}
print d.keys() # ['a', 'b']
# d.values() -> list: copy of d's list of values
d = {'a': 1, 'b': 2}
print d.values() # [1, 2]
# d.get(key,defval) -> value: d[key] if key in d, else defval
d = {'a': 1, 'b': 2}
print d.get("c", 3) # 3
print d # {'a': 1, 'b': 2}
# d.setdefault(key[,defval=None]) -> value: if key not in d set d[key]=defval, return d[key]
d = {'a': 1, 'b': 2}
print 'd.setdefault("c", []) returns ' + str(d.setdefault("c", 3)) + ' d is now ' + str(d)
# d.setdefault("c", []) returns 3 d is now {'a': 1, 'c': 3, 'b': 2}
#d.pop(key[,defval]) -> value: del key and returns the corresponding value. If key is not found, defval is returned if given, otherwise KeyError is raised
d = {'a': 1, 'b': 2}
print 'd.pop("b", 3) returns ' + str(d.pop("b", 3)) + ' d is now ' + str(d)
# d.pop("b", 3) returns 2 d is now {'a': 1}
print 'd.pop("c", 3) returns ' + str(d.pop("c", 3)) + ' d is still ' + str(d)
# d.pop("c", 3) returns 3 d is still {'a': 1}
# sort on values
import operator
x = {1: 4, 5: 4, 4: 4}
sorted_x = sorted(x.items(), key=operator.itemgetter(1), reverse=True)
print 'sorted(x.items(), key=operator.itemgetter(1)) sorts on values ' + str(sorted_x)
# sort on keys instead of values:
import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))
# max of values
d = {'a':1000, 'b':3000, 'c': 100}
print 'key of max value is ' + max(d.keys(), key=(lambda key: d[key]))
# inverse a map
for k,v in d.iteritems():
print k
print v
print 'inverse of d is ' + str(dict((v, k) for k, v in d.iteritems()))
# use-cases for setdefault
data = [(1, "a"), (2, "b")]
# really verbose
new = {}
for (key, value) in data:
if key in new.keys():
new[key].append( value )
else:
new[key] = [value]
# easy with setdefault
new = {}
for (key, value) in data:
group = new.setdefault(key, []) # key might exist already
group.append( value )
# even simpler with defaultdict
from collections import defaultdict
new = defaultdict(list)
for (key, value) in data:
new[key].append( value ) # all keys have a default already