[英]Merging two dictionaries in Python with a key consisting of two values
I have data like -- 我有类似的数据
sample 1, domain 1, value 1
sample 1, domain 2, value 1
sample 2, domain 1, value 1
sample 2, domain 3, value 1
-- stored in a dictionary -- -存储在字典中-
dict_1 = {('sample 1','domain 1'): value 1, ('sample 1', 'domain 2'): value 1}
-- etc. -等
Now, I have a different kind of value, named value 2 -- 现在,我有另一种值,称为值2:
sample 1, domain 1, value 2
sample 1, domain 2, value 2
sample 2, domain 1, value 2
sample 2, domain 3, value 2
-- which I again put in a dictionary, -我再次放入字典中
dict_2 = {('sample 1','domain 1'): value 2, ('sample 1', 'domain 2'): value 2}
How can I merge these two dictionaries in python? 如何在python中合并这两个字典? The keys, for instance ('sample 1', 'domain 1')
are the same for both dictionaries. 例如,两个字典的键('sample 1', 'domain 1')
相同。
I expect it to look like -- 我希望它看起来像-
final_dict = {('sample 1', 'domain 1'): (value 1, value 2), ('sample 1', 'domain 2'): (value 1, value 2)}
-- etc. -等
The closest you're likely to get to this would be a dict
of list
s (or set
s). 您可能最接近的是list
(或set
)的dict
。 For simplicity, you usually go with collections.defaultdict(list)
so you're not constantly checking if the key already exists. 为简单起见,通常使用collections.defaultdict(list)
因此不必经常检查密钥是否已存在。 You need to map to some collection type as a value because dict
s have unique keys, so you need some way to group the multiple values you want to store for each key. 您需要将某种类型的集合映射为一个值,因为dict
具有唯一的键,因此您需要某种方式来分组要为每个键存储的多个值。
from collections import defaultdict
final_dict = defaultdict(list)
for d in (dict_1, dict_2):
for k, v in d.items():
final_dict[k].append(v)
Or equivalently with itertools.chain
, you just change the loop to: 或等效地使用itertools.chain
,只需将循环更改为:
from itertools import chain
for k, v in chain(dict_1.items(), dict_2.items()):
final_dict[k].append(v)
Side-note: If you really need it to be a proper dict
at the end, and/or insist on the values being tuple
s rather than list
s, a final pass can convert to such at the end: 旁注:如果您确实需要在最后将其作为适当的dict
,并且/或者坚持使用值是tuple
而不是list
,那么最后的传递可以在末尾转换为:
final_dict = {k: tuple(v) for k, v in final_dict.items()}
You can use set intersection of keys to do this: 您可以使用设置键的交集来做到这一点:
dict_1 = {('sample 1','domain 1'): 'value 1', ('sample 1', 'domain 2'): 'value 1'}
dict_2 = {('sample 1','domain 1'): 'value 2', ('sample 1', 'domain 2'): 'value 2'}
result = {k: (dict_1.get(k), dict_2.get(k)) for k in dict_1.keys() & dict_2.keys()}
print(result)
# {('sample 1', 'domain 1'): ('value 1', 'value 2'), ('sample 1', 'domain 2'): ('value 1', 'value 2')}
The above uses dict.get()
to avoid possibilities of a KeyError being raised(very unlikely), since it will just return None
by default. 上面使用dict.get()
避免了引发KeyError的可能性(极不可能),因为默认情况下它只会返回None
。
As @ShadowRanger suggests in the comments, If a key is for some reason not found, you could replace from the opposite dictionary: 正如@ShadowRanger在评论中建议的那样,如果由于某种原因未找到键,则可以从相对的字典中替换:
{k: (dict_1.get(k, dict_2.get(k)), dict_2.get(k, dict_1.get(k))) for k in dict_1.keys() | dict_2.keys()}
Does something handcrafted like this work for you? 这样手工制作的东西对您有用吗?
dict3 = {}
for i in dict1:
dict3[i] = (dict1[i], dict2[i])
from collections import defaultdict
from itertools import chain
dict_1 = {('sample 1','domain 1'): 1, ('sample 1', 'domain 2'): 2}
dict_2 = {('sample 1','domain 1'): 3, ('sample 1', 'domain 2'): 4}
new_dict_to_process = defaultdict(list)
dict_list=[dict_1.items(),dict_2.items()]
for k,v in chain(*dict_list):
new_dict_to_process[k].append(v)
Output will be 输出将是
{('sample 1', 'domain 1'): [1, 3],
('sample 1', 'domain 2'): [2, 4]})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.