如何最好地合并多个词典中的值? [英] How best to merge the values from multiple dictionaries?

查看:90
本文介绍了如何最好地合并多个词典中的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个函数,该函数接受字典的多个参数,并返回一个串联的字典。我在网上研究了一段时间以合并合并的词典,并测试了有趣的词典。它们都导致更新值(或覆盖它们)。

I created a function that accepts multiple arguments of dictionaries, and returns a concatenated dictionary. I researched online for a while about concatenating a merging dictionaries and tested the interesting ones. They all resulted in updating the values (or overwriting them).

我的用例是传递字典,其中每个键都有一个值,并希望使用具有相同或不同键的字典,以及每个键的值列表。这就是我对字典的所谓串联的外观的定义。

My use case is passing in dictionaries where each key has a single value, and want a dictionary with the same or different keys, with a list of values for each key. That is my definition of what a so-called "concatenation" of dictionaries would look like.

以下是两个非常基本的字典:

Here are two very basic dictionaries:

a = {1: 'a', 2: 'b', 3: 'c'}
b = {1: 'd', 2: 'e', 3: 'f'}

此处是函数:

def merge_dict(*args:dict):

    result = {}

    for arg in args:

        if not isinstance(arg, dict):
            return {}

        result_keys = result.keys()
        for key, value in arg.items():
            if key not in result_keys:
                result[key] = [value]
            else:
                result[key].append(value)

    return result

输出为:

print(merge_dict(a, b))
{1: ['a', 'd'], 2: ['b', 'e'], 3: ['c', 'f']}

我可以对元组或数组,Numpy数组等执行相同的操作。请注意,此函数非常简单,不会清理输入或验证数据结构而不是 dict 实例。

I could do the same for tuples, or arrays, Numpy arrays, etc. Note this function is very simple and doesn't sanitize input or validate the data structure further than it being a dict instance.

但是,我想知道是否有更有效的方法或 pythonic方式。请随时添加您的输入。

But, I would like to know if there is a more efficient or "pythonic" way of doing this. Please feel free to add your input.

考虑使用不同的键添加这些词典:

Consider adding these dictionaries with different keys:

c = {4: 'g', 5: 'h', 6: 'i'}
d = {4: 'j', 5: 'k', 6: 'l'}

输出为:

print(merge_dict(a, b, c, d))
{1: ['a', 'd'], 2: ['b', 'e'], 3: ['c', 'f'], 4: ['g', 'j'], 5: ['h', 'k'], 6: ['i', 'l']}

我将很快处理嵌套数据结构。

I will work on nested data structures soon.

由于您的回答,这就是我做到了:

Because of your answers, here is what I did:

import collections

def merge_dicts_1(*args):
    rtn = collections.defaultdict(list)
    for input_dict in args:
        for key, value in input_dict.items():
            rtn[key].append(value)
    return rtn

def merge_dicts_2(*args):
    rtn = {}
    for input_dict in args:
        for key, value in input_dict.items():
            rtn.setdefault(key, []).append(value)
    return rtn

if __name__ == "__main__":
    a = {1: 'a', 2: 'b', 3: 'c'}
    b = {1: 'd', 2: 'e', 3: 'f'}
    c = {4: 'g', 5: 'h', 6: 'i'}
    d = {4: 'j', 5: 'k', 6: 'l'}
    e = merge_dicts_1(a, b, c, d)
    f = merge_dicts_2(a, b, c, d)
    print(e)
    print(f)
    print(e == f)

打印以下内容:

defaultdict(<class 'list'>, {1: ['a', 'd'], 2: ['b', 'e'], 3: ['c', 'f'], 4: ['g', 'j'], 5: ['h', 'k'], 6: ['i', 'l']})
{1: ['a', 'd'], 2: ['b', 'e'], 3: ['c', 'f'], 4: ['g', 'j'], 5: ['h', 'k'], 6: ['i', 'l']}
True

谢谢!

推荐答案

类似这样的东西可以用于任意数量的输入字典:

Something like this would work for any number of input dictionaries:

import collections

def merge_dicts(*args):
    rtn = collections.defaultdict(list)
    for input_dict in args:
        for key, value in input_dict.items():
            rtn[key].append(value)
    return rtn

诀窍是使用 defaultdict 结构,以在不存在新条目时自动进行创建。在这种情况下,访问尚不存在的密钥会将其创建为空列表。

The trick is using the defaultdict structure to automatically make new entries when they don't exist. In this case, accessing a key that doesn't yet exist creates it as an empty list.

请注意,以上代码返回了 defaultdict 对象。如果不希望这样,则可以将其强制转换为dict或使用以下函数:

Note that the above returns a defaultdict object. If this isn't desirable, you can cast it back to dict or use this function instead:

def merge_dicts(*args):
    rtn = {}
    for input_dict in args:
        for key, value in input_dict.items():
            rtn.setdefault(key, []).append(value)
    return rtn

这篇关于如何最好地合并多个词典中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆