我有一个空列表的嵌套目录:
mom_fut = {'M2K': {'comm': [], 'mtm': []},
'MNQ': {'comm': [], 'mtm': []},
'NG': {'comm': [], 'mtm': []},
'QM': {'comm': [], 'mtm': []},
'VIX': {'comm': [], 'mtm': []},
'ZS': {'comm': [], 'mtm': []},
'ZW': {'comm': [], 'mtm': []}}
我正在遍历可能包含主dict键的字符串列表。如果主键存在,我将相应的值(来自单独的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]}})
但我需要使用列表和追加,因为每个键可能有多个实例,所以每个嵌套列表中可能有多个值
问题解决了
问题在于这本词典的创建方式。我使用了dict.fromkeys(),但我不知道它实际上“对所有值使用相同的对象…这意味着,所有键共享同一个空列表…当您尝试添加到一个列表的值时,会对该对象进行适当的更改,因此引用它的所有人都可以看到对该对象的更改。”请参见Append value to one list in dictionary appends value to all lists in dictionary
通过使用dict comprehension来创建dict,问题得到了解决,迭代和追加工作也如期进行
相关问题 更多 >
编程相关推荐