stock = {
"orange": 3,
"apple": 1,
}
d = {
1: "Numerical value",
date(2016, 1, 1): "date"
(1, 2): "Tuple",
None: "None"
}
--Any hashable and immutable object can be used --It is OK even if multiple types are mixed in one dict --None is OK
--Tuple and compound key --Use numeric keyed dict instead of list
stock = {
("Tokyo branch", "orange"): 3,
("Tokyo branch", "apple"): 1,
("Nagoya Branch", "orange"): 8,
}
stock["Nagoya Branch", "apple"] = 4
--It's okay if the key is missing
python
a = {
1: "apple",
3: "orange",
}
--Effective when combined with defaultdict
python
from collections import defaultdict
a = defaultdict(str)
a[1] = "apple"
a[3] = "orange"
l = max(l.keys())
for i in range(0, l + 1):
print("%d: %s" % (i, a[i]))
0:
1: apple
2:
3: orange
Recommended Posts