python Python中的递归字典合并

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Python中的递归字典合并相关的知识,希望对你有一定的参考价值。

import collections

def dict_merge(dct, merge_dct):
    """ Recursive dict merge. Inspired by :meth:``dict.update()``, instead of
    updating only top-level keys, dict_merge recurses down into dicts nested
    to an arbitrary depth, updating keys. The ``merge_dct`` is merged into
    ``dct``.

    :param dct: dict onto which the merge is executed
    :param merge_dct: dct merged into dct
    :return: None
    """
    for k, v in merge_dct.iteritems():
        if (k in dct and isinstance(dct[k], dict)
                and isinstance(merge_dct[k], collections.Mapping)):
            dict_merge(dct[k], merge_dct[k])
        else:
            dct[k] = merge_dct[k]

以上是关于python Python中的递归字典合并的主要内容,如果未能解决你的问题,请参考以下文章

python 递归地将字典与boltons.iterutils.remap合并。对于@ hynek的配置很有用。 https://twitter.com/hynek/status/6967205930

python - 如何在更改python中的值时合并一个嵌套字典和一个简单字典?

在Python中合并字典的层次结构

python 字典合并,字典取值,列表转字典

Python递归替换嵌套字典键中的字符?

在python中循环遍历数据框字典并将字典中的每个数据框与单个数据框合并