forked from cirosantilli/python-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdict.py
More file actions
executable file
·184 lines (117 loc) · 3.8 KB
/
dict.py
File metadata and controls
executable file
·184 lines (117 loc) · 3.8 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
"""
## dict
## Map
Unordered map of hashable keys and to values of any type.
"""
if '## create':
# Built-in constructor syntax:
d = {1: 'a', 'b': 2, 1.1: 2}
# Global factory function.
# From list of pairs:
assert dict([(0, 'zero'), (1, 'one')]) == {0: 'zero', 1: 'one'}
# From kwargs (keys can only be strings):
assert dict(zero=0, one=1) == {'zero': 0, 'one': 1}
# dict comprehension:
assert {key: value for (key, value) in [(1, 2), (3, 4)]} == {1: 2, 3: 4}
# Keys must be hashable. This excludes all mutable built-in types like lists.
try:
d = {[1]: 2}
except TypeError:
pass
else:
assert False
if '## fromkeys':
# <https://docs.python.org/2/library/stdtypes.html#dict.fromkeys>
# Create a dictionary with keys from a list, and a single value.
# `dict` here is the `dict` type.
assert dict.fromkeys([1, 2, 3]) == {1: None, 2: None, 3: None}
assert dict.fromkeys([1, 2, 3], 0) == {1: 0, 2: 0, 3: 0}
# Note however that this is not usually what you want for mutable objects:
# http://stackoverflow.com/questions/8174723/dictionary-creation-with-fromkeys-and-mutable-objects-a-surprise
# To list of pairs:
d = {1: 'one', 2: 'two'}
assert set(d.items()) == set([(1, 'one'), (2, 'two')])
# Get list of keys (undefined order)
d = {1: 'one', 2: 'two'}
assert set(d.keys()) == set([1, 2])
# Why a list is returned by keys() instead of set():
# http://stackoverflow.com/questions/13886129/why-does-pythons-dict-keys-return-a-list-and-not-a-set
# set() didn't exist yet!
# To string:
print "dict str() = "
print d
# Undefined output because undefined key order.
# Get value of key:
d = {1: 'one', 2: 'two'}
assert d[1] == 'one'
# If not in dict, `KeyError` exception:
d = {}
try:
d['not-a-key']
except KeyError:
pass
else:
assert False
# Check if key is in dict:
d = {1: 2}
if 1 in d:
pass
else:
assert False
if 2 in d:
assert False
# Get default value if not present:
assert d.get('not-a-key', 'default value') == 'default value'
# Add new pair:
d= {}
d[0] = 'zero'
assert d == {0: 'zero'}
# Remove pair:
d= {0: 'zero'}
del d[0]
# If key not present, KeyError:
try:
del d[0]
except KeyError:
pass
else:
assert False
# Add new pair if key not present:
d = {1: 2}
assert d.setdefault(1, 3) == 2
assert d == {1: 2}
d = {}
assert d.setdefault(1, 3) == 3
assert d == {1: 3}
if '## update':
# Add update all keys on d0 with those of d1:
d0 = {0: 'zero', 1: 'one'}
d1 = {0: 'zero2', 2: 'two'}
assert d0.update(d1) is None
assert d0 == {0: 'zero2', 1: 'one', 2: 'two'}
# Create a new dict that is the union of two other dicts:
d0 = {0: 'zero', 1: 'one'}
d1 = {0: 'zero2', 2: 'two'}
d01 = d0.copy()
assert d01.update(d1) is None
assert d01 == {0: 'zero2', 1: 'one', 2: 'two'}
if 'Iterate / loop over dict':
# Unspecified order. Python 3 has `collections.OrderedDict` (backported to 2.7).
# Keys only:
assert sorted([i for i in {1:-1, 2:-2}]) == [1, 2]
if '##iteritems':
# Keys value pairs:
assert sorted([(i,j) for i,j in {1:-1, 2:-2}.iteritems()]) == [(1, -1), (2, -2)]
# Iteritems sorted by key. Must pull all into memory first.
assert [(i,j) for i,j in sorted({2:-2, 1:-1}.iteritems())] == [(1, -1), (2, -2)]
# Iteritems is out of Python3. Items is present in both 2 and 3
# but returns a list, not an iterator. 2to3 converts it automatically.
if '## filter':
"""
Only keys in a list:
http://stackoverflow.com/questions/6827834/how-to-filter-a-dict-to-contain-only-keys-in-a-given-list
Arbitrary function:
http://stackoverflow.com/questions/2844516/python-filter-a-dictionary
Comprehensions are the only way it seems.
"""