合并两个具有相同键和值的字典

合并两个具有相同键和值的字典

本文介绍了Python 3.x:合并两个具有相同键和值的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python版本:3.x

Python version: 3.x

我有两个具有相同键的字典,其值是数组.出于必要的目的,我在这里看到的大多数问题的每个键都只有一个值.我想要的是将这两个字典与值合并为连接数组.也许下面会清除:

I have two dictionaries with same keys and the values are arrays. Most of the questions I saw here, for the required purpose, have only one value for each key. What I want is to merge those two dictionaries with values as joined array. Maybe below would clear:

我所拥有的:

d1 = {(1, "Autumn"): np.array([2.5, 4.5, 7.5, 9.5]), (1, "Spring"): np.array([10.5, 11.7, 12.3, 15.0])}
d2 = {(1, "Autumn"): np.array([10.2, 13.3, 15.7, 18.8]), (1, "Spring"): np.array([15.6, 20, 23, 27])}

我尝试过:

d3 = {**d1, **d2}

我想要什么:

d3 = {(1, "Autumn"): np.array([2.5, 4.5, 7.5, 9.5, 10.2, 13.3, 15.7, 18.8]), (1, "Spring"): np.array([10.5, 11.7, 12.3, 15.0, 15.6, 20, 23, 27])}

我在这里错过了什么吗?请帮忙!

Am I missing something here? Please help!

PS:我看过如何合并两个单个表达式中的字典?.

推荐答案

尝试一下:

>>> import numpy as np
>>> d1 = {(1, "Autumn"): np.array([2.5, 4.5, 7.5, 9.5]), (1, "Spring"): np.array([10.5, 11.7, 12.3, 15.0])}
>>> d2 = {(1, "Autumn"): np.array([10.2, 13.3, 15.7, 18.8]), (1, "Spring"): np.array([15.6, 20, 23, 27])}
>>> d3 = {k: np.concatenate((d1.get(k, np.array([])), d2.get(k, np.array([])))) for k in set(d1.keys()).union(set(d2.keys()))}
>>> d3
{(1, 'Spring'): array([10.5, 11.7, 12.3, 15. , 15.6, 20. , 23. , 27. ]), (1, 'Autumn'): array([ 2.5,  4.5,  7.5,  9.5, 10.2, 13.3, 15.7, 18.8])}

注释:

  • 这是 dict 理解
  • 首先,计算2个 dict 中键的并集,以确保不留键(为此,每个 dict 中的键转换为 set )
  • 对于上述集合中的每个元素,请从每个 dict 中获取相应的数组(如果不存在键,则为空),并将它们连接起来
  • 这是 Python ic(也是 general )方法,我的 numpy 知识接近 0 (我敢肯定,从代码片段中可以很明显地看出来-加上所有括号后,它看起来非常复杂),很可能 numpy 可以在其中添加内容更优雅的方式
  • [SO]:如何将两个字典合并到一个单个表达式?所需的输出和当前的表达式(考虑到 dict 的值只是可迭代的(无论它们是 Python 还是 numpy 或任何其他无关紧要的内容))是在常见情况下 merge 概念关于 dict 的2种不同(且同样正确)的方法按键:
    • 一个只保留最后一个 dict
    • 中的值
    • 所有其他总和(无论 sum 对操作数意味着什么)
    • It's a dict comprehension
    • First, a union of the keys in the 2 dicts is computed, to make sure that no key is left aside (for that, the keys in each dict are converted into a set)
    • For each element in the above set, get the corresponding array (empty one if the key is not present) from each dict, and concatenate them
    • This is the Pythonic (and also general) approach, my numpy knowledge is somewhere close to 0 (I'm sure that it's pretty obvious from the code snippet - it looks awfully complex with all those parentheses), it's extremely likely that numpy has something to make things in a much more elegant manner
    • [SO]: How to merge two dictionaries in a single expression? desired output and the current one (considering that the dict values are simply iterables (whether they are Python or numpy or any other kind is irrelevant)) are 2 different (and equally correct) approaches of the merge concept regarding dicts, in case of common keys:
      • One only keeps the value from the last dict
      • The other sums (whatever sum would mean for the operands) all of them

      这篇关于Python 3.x:合并两个具有相同键和值的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:11