0

为什么这段代码不起作用?它返回 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'
4

5 回答 5

2

如果我理解正确,使用dict理解相当简单:

>>> pprint({k:{'transferred':v['b']}  for k, v in adict.iteritems()})
{'key1': {'transferred': 1000},
 'key2': {'transferred': 10},
 'key3': {'transferred': 250},
 'key4': {'transferred': 200},
 'key5': {'transferred': 409},
 'key6': {'transferred': 350}}

您还可以执行便利功能以仅保留某些子键(如果它们的名称相同)

>>> from operator import itemgetter
>>> def dict_with_subkeys(odict, *keys):
    return {k:dict(zip(keys, itemgetter(*keys)(odict[k]))) for k in odict}

>>> pprint(dict_with_subkeys(adict, 'a', 'c'))
{'key1': {'a': 0.078, 'c': 100},
 'key2': {'a': 0.0645, 'c': 5},
 'key3': {'a': 0.0871, 'c': 45},
 'key4': {'a': 0.0842, 'c': 37},
 'key5': {'a': 0.054, 'c': 82},
 'key6': {'a': 0.055, 'c': 60}}
于 2012-12-23T17:19:48.603 回答
2

another_dict[k]您必须在访问它之前创建一个新字典:

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:
    if k not in another_dict:
        another_dict[k] = {}
    another_dict[k]['transferred'] = adict[k]['b']
于 2012-12-23T17:14:58.383 回答
1

使用another_dict[k]['transferred'],您正在尝试访问尚未创建'transferred'的 dict 的密钥。key k

您可以defaultdict在这里使用:-

from collections import defaultdict
another_dict = defaultdict(dict)

修改代码:-

>>> from collections import defaultdict
>>> 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 = defaultdict(dict)
... for k in adict:
...     another_dict[k]['transferred'] = adict[k]['b']

>>> another_dict
5: defaultdict(<type 'dict'>, {'key3': {'transferred': 250}, 
                           'key2': {'transferred': 10}, 
                           'key1': {'transferred': 1000}, 
                           'key6': {'transferred': 350}, 
                           'key5': {'transferred': 409}, 
                           'key4': {'transferred': 200}})
于 2012-12-23T17:13:26.723 回答
1

问题another_dict[k]实际上还不存在,而您正在尝试做another_dict[k]['transferred']一些甚至没有初始化的事情。所以你需要先初始化它:

In [35]: 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}}

In [36]: another_dict={}

In [37]: for k in adict:
    another_dict[k]={}                          #initialize another_dict[k]
    another_dict[k]['transferred']=adict[k]['b']

In [38]: another_dict
Out[38]: 
{'key1': {'transferred': 1000},
 'key2': {'transferred': 10},
 'key3': {'transferred': 250},
 'key4': {'transferred': 200},
 'key5': {'transferred': 409},
 'key6': {'transferred': 350}}
于 2012-12-23T17:14:53.560 回答
0

谢谢@Ashwini 我明白你的意思。还有@Jon。

随着我在 Python 和(“字典理解”?)方面的进步,我发现的另一个解决方案是:

another_dict = {k:{'b':adict[k]['b']} for k in adict}
于 2012-12-23T17:45:14.043 回答