为什么这段代码不起作用?它返回 key3 的键错误。adict 有 key3,它似乎与我向空字典添加键的方式没有什么不同,只是我在循环中相互传递它们。如何将一个字典中的键和值传递到具有相同主键的新字典中?
adict = {'key1':{'a':.078, 'b':1000, 'c':100},
'key2':{'a':.0645, 'b':10, 'c':5},
'key3':{'a':.0871, 'b':250, 'c':45},
'key4':{'a':.0842, 'b':200, 'c':37},
'key5':{'a':.054, 'b':409, 'c':82},
'key6':{'a':.055, 'b':350, 'c':60}}
another_dict = {}
for k in adict:
another_dict[k]['transferred'] = adict[k]['b']
>>> Traceback (most recent call last):
File "C:\Python27\test.py", line 26, in <module>
another_dict[k]['transferred'] = adict[k]['b']
KeyError: 'key3'