合并两个字典并替换缺失的数值。

huangapple go评论61阅读模式
英文:

Combine two dicts and replace missing values

问题

我正在寻求将两个字典组合起来,通过将具有相同键的元素分组,但我也希望考虑那些不共享在两个字典之间的键。例如,给定以下两个字典。

d1 = {'a': 1, 'b': 2, 'c': 3, 'e': 5}
d2 = {'a': 11, 'b': 22, 'c': 33, 'd': 44}

预期的代码将输出

df = {'a': [1, 11], 'b': [2, 22], 'c': [3, 33], 'd': [0, 44], 'e': [5, 0]}

或类似的数组:

df = [['a', 1, 11], ['b', 2, 22], ['c', 3, 33], ['d', 0, 44], ['e', 5, 0]]

具体使用 0 来表示不存在的条目并不重要,可以使用任何字符来表示缺失的值。

我尝试使用以下代码:

df = defaultdict(list)
for d in (d1, d2): 
    for key, value in d.items():
        df[key].append(value)

但得到以下结果:

df = {'a': [1, 11], 'b': [2, 22], 'c': [3, 33], 'd': [44], 'e': [5]}

这并没有告诉我哪个字典缺少了条目。我可以回头查看它们两个,但我正在寻找一个更优雅的解决方案。

英文:

I am looking to combine two dictionaries by grouping elements that share common keys, but I would also like to account for keys that are not shared between the two dictionaries. For instance given the following two dictionaries.

d1 = {'a':1, 'b':2, 'c': 3, 'e':5}
d2 = {'a':11, 'b':22, 'c': 33, 'd':44}

The intended code would output

df = {'a':[1,11] ,'b':[2,22] ,'c':[3,33] ,'d':[0,44] ,'e':[5,0]}

Or some array like:

df = [[a,1,11] , [b,2,22] , [c,3,33] , [d,0,44] , [e,5,0]]

The fact that I used 0 specifically to denote an entry not existing is not important per se. Just any character to denote the missing value.

I have tried using the following code

df = defaultdict(list)
for d in (d1, d2): 
    for key, value in d.items():
        df[key].append(value)

But get the following result:

df = {'a':[1,11] ,'b':[2,22] ,'c':[3,33] ,'d':[44] ,'e':[5]}

Which does not tell me which dict was missing the entry.

I could go back and look through both of them, but was looking for a more elegant solution

答案1

得分: 3

你可以使用字典推导式来实现,代码如下:

d1 = {'a': 1, 'b': 2, 'c': 3, 'e': 5}
d2 = {'a': 11, 'b': 22, 'c': 33, 'd': 44}
res = {k: [d1.get(k, 0), d2.get(k, 0)] for k in set(d1).union(d2)}
print(res)
英文:

You can use a dict comprehension like so:

d1 = {'a':1, 'b':2, 'c': 3, 'e':5}
d2 = {'a':11, 'b':22, 'c': 33, 'd':44}
res = {k: [d1.get(k, 0), d2.get(k, 0)] for k in set(d1).union(d2)}
print(res)

答案2

得分: 2

另一种解决方案:

d1 = {"a": 1, "b": 2, "c": 3, "e": 5}
d2 = {"a": 11, "b": 22, "c": 33, "d": 44}

df = [[k, d1.get(k, 0), d2.get(k, 0)] for k in sorted(d1.keys() | d2.keys())]
print(df)

输出:

[['a', 1, 11], ['b', 2, 22], ['c', 3, 33], ['d', 0, 44], ['e', 5, 0]]


如果不需要排序的结果,可以去掉 sorted()

英文:

Another solution:

d1 = {"a": 1, "b": 2, "c": 3, "e": 5}
d2 = {"a": 11, "b": 22, "c": 33, "d": 44}

df = [[k, d1.get(k, 0), d2.get(k, 0)] for k in sorted(d1.keys() | d2.keys())]
print(df)

Prints:

[['a', 1, 11], ['b', 2, 22], ['c', 3, 33], ['d', 0, 44], ['e', 5, 0]]

If you do not want sorted results, leave the sorted() out.

huangapple
  • 本文由 发表于 2023年2月14日 08:18:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75442369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定