追加到嵌套字典列表

时间:2020-02-17 17:58:06

标签: python dictionary nested append

我有一个空列表的嵌套字典:

mom_fut = {'M2K': {'comm': [], 'mtm': []},
           'MNQ': {'comm': [], 'mtm': []},
           'NG': {'comm': [], 'mtm': []},
           'QM': {'comm': [], 'mtm': []},
           'VIX': {'comm': [], 'mtm': []},
           'ZS': {'comm': [], 'mtm': []},
           'ZW': {'comm': [], 'mtm': []}}

我正在遍历可能包含主字典键的字符串列表。如果存在主键,则将相应的值(来自单独的df)附加到各自的嵌套列表(“ comm”和“ mtm”)中:

for des in mom_fut_des:
    symb, mat = des.split()
        mom_fut[symb]['comm'].append(mtm_fut_mom.commissions[mtm_fut_mom.description == des].sum())
        mom_fut[symb]['mtm'].append(mtm_fut_mom.total[mtm_fut_mom.description == des].sum())

但是,不要将相应的值附加到各自的键值列表中,而是返回我想要的(和期望的)填充的dict:

{'M2K': {'comm': [-25.5777713], 'mtm': [-217.5835163]},
'MNQ': {'comm': [-1.8012515], 'mtm': [-3.7174765]},
 'NG': {'comm': [-12.7160691], 'mtm': [-326.9769691]},
 'QM': {'comm': [-1.5866343], 'mtm': [-49.4922593]},
 'VIX': {'comm': [-1.8242462], 'mtm': [-97.6354962]},
 'ZS': {'comm': [-12.9690108], 'mtm': [-415.3762608]},
 'ZW': {'comm': [-15.1305126], 'mtm': [-235.4963876]}}

它将所有值返回到每个键的相应嵌套列表中:

{'M2K': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'MNQ': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'NG': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'QM': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'VIX': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'ZS': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]},
 'ZW': {'comm': [-25.5777713,
   -1.8012515,
   -12.7160691,
   -1.5866343,
   -12.9690108,
   -1.8242462,
   -15.1305126],
  'mtm': [-217.5835163,
   -3.7174765,
   -326.9769691,
   -49.4922593,
   -415.3762608,
   -97.6354962,
   -235.4963876]}}

我可以通过调整代码并使用dict.update()获得“正确的”输出

for des in mom_fut_des:
    symb, mat = des.split()
    comm = mtm_fut_mom.commissions[mtm_fut_mom.description == des].sum()
    mtm = mtm_fut_mom.total[mtm_fut_mom.description == des].sum()
    mom_fut.update({symb:{'comm':[comm],'mtm':[mtm]}})

但是我需要使用列表和追加,因为每个键可能有多个实例,因此每个嵌套列表中可能有多个值。

1 个答案:

答案 0 :(得分:0)

问题解决了。

问题在于字典的创建方式。我曾经不知道实际上使用了dict.fromkeys()“对所有值使用相同的对象...这意味着,所有键共享相同的空列表...当您尝试附加到一个列表的值时,这些更改是在对象的原位进行的,因此对其的更改对于所有引用该对象的用户都是可见的。”参见Append value to one list in dictionary appends value to all lists in dictionary

通过使用dict理解而不是dict来创建dict,该问题已得到解决,迭代和附加按预期工作。