import collections
d = collections.defaultdict(dict)
d["i"]["a"] = "111"
d["i"]["b"] = "222"
print d
od = collections.OrderedDict()
od["i"]["a"] = "111"
od["i"]["b"] = "222"
print od
输出:
defaultdict(<type 'dict'>, {'i': {'a': '111', 'b': '222'}})
Traceback (most recent call last):
File "app_main.py", line 51, in run_toplevel
File "/Users/adam/Desktop/collections.py", line 12, in <module>
od["i"]["a"] = "111"
KeyError: 'i'
为什么 OrderedDict 的关键错误以及我能做些什么?
谢谢。