合并字典而不覆盖以前的值,其中 value 是一个列表 [英] Merge dictionaries without overwriting previous value where value is a list
问题描述
我知道合并字典而不覆盖值,合并几个"python字典,如何合并多个具有相同键的字典? 和 如何在一个表达式中合并两个 Python 字典?.
不过,我的问题略有不同.
假设我有这三本词典
dict_a = {'a': [3.212], 'b': [0.0]}dict_b = {'a': [923.22, 3.212], 'c': [123.32]}dict_c = {'b': [0.0]}
合并后的结果应该是
result_dict = {'a': [3.212, 3.212, 923.22], 'b': [0.0, 0.0], 'c': [123.32]}
这里的代码有效,但会在列表中嵌套列表
result_dict = {}dicts = [dict_a, dict_b, dict_c]对于字典中的 d:对于 d.iteritems() 中的 k, v:result_dict.setdefault(k, []).append(v)
使用 extend
而不是 append
会阻止嵌套列表,但如果键尚不存在则不起作用.所以基本上它应该在没有覆盖的情况下执行 update
,就像在其他线程中一样.
抱歉,这是我的错误.昨天太晚了,我没有注意到抛出错误的行不是我认为的那样,因此假设我的字典已经具有上述结构.事实上,mgilson
是正确的,假设它与 TypeError
相关.准确地说,是一个可统一的浮动".
我很确定 .extend
在这里工作......
神奇之处在于 dict.setdefault
方法.如果该键不存在,setdefault
添加一个具有您提供的默认值的新键.然后它返回那个可以修改的默认值.
请注意,如果项目 v
不可迭代,则此解决方案会有问题.也许这就是你的意思?例如如果 dict_a = {'a': [3.212], 'b': 0.0}
.
在这种情况下,我认为您需要在 try-except
子句中捕获 TypeError: type object is not iterable
:
for d in dicts:对于 d.iteritems() 中的 k, v:尝试:result_dict.setdefault(k, []).extend(v)除了类型错误:result_dict[k].append(v)
I am aware of Merge dictionaries without overwriting values, merging "several" python dictionaries, How to merge multiple dicts with same key?, and How to merge two Python dictionaries in a single expression?.
My problem is slightly different, however.
Lets say I have these three dictionaries
dict_a = {'a': [3.212], 'b': [0.0]}
dict_b = {'a': [923.22, 3.212], 'c': [123.32]}
dict_c = {'b': [0.0]}
The merged result should be
result_dict = {'a': [3.212, 3.212, 923.22], 'b': [0.0, 0.0], 'c': [123.32]}
This code here works, but would nest lists within the lists
result_dict = {}
dicts = [dict_a, dict_b, dict_c]
for d in dicts:
for k, v in d.iteritems():
result_dict.setdefault(k, []).append(v)
Using extend
instead of append
would prevent the nested lists, but doesn't work if a key doesn't exist yet. So basically it should do a update
without the overwriting, as in the other threads.
Sorry, it was a mistake on my side. It was too late yesterday and I didn't notice the line that threw the error wasn't the one I thought it did, therefore assumed my dictionaries would already have the above structure.
In fact, mgilson
was correct assuming that it was related to a TypeError
. To be exact, an 'uniterable float'.
I'm pretty sure that .extend
works here ...
>>> dict_a = {'a': [3.212], 'b': [0.0]}
>>> dict_b = {'a': [923.22, 3.212], 'c': [123.32]}
>>> dict_c = {'b': [0.0]}
>>> result_dict = {}
>>> dicts = [dict_a, dict_b, dict_c]
>>>
>>> for d in dicts:
... for k, v in d.iteritems():
... result_dict.setdefault(k, []).extend(v)
...
>>> result_dict
{'a': [3.212, 923.22, 3.212], 'c': [123.32], 'b': [0.0, 0.0]}
The magic is in the dict.setdefault
method. If the key doesn't exist, setdefault
adds a new key with the default value you provide. It then returns that default value which can then be modified.
Note that this solution will have a problem if the item v
is not iterable. Perhaps that's what you meant? e.g. if dict_a = {'a': [3.212], 'b': 0.0}
.
In this case, I think you'll need to resort to catching the TypeError: type object is not iterable
in a try-except
clause:
for d in dicts:
for k, v in d.iteritems():
try:
result_dict.setdefault(k, []).extend(v)
except TypeError:
result_dict[k].append(v)
这篇关于合并字典而不覆盖以前的值,其中 value 是一个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!