如何将Python嵌套字典转换为非嵌套字典?

4
如何转换以下src字典(嵌套字典)
{
    'a':{'b':1, 'c':{'d':2}},
    'b':3,
    'c':{'d':4, 'a':5}
}

将目标字典(非嵌套)放置在下面?
{
    'a.b':1,
    'a.c.d':2,
    'b':3,
    'c.d':4,
    'c.a':5
}

src字典是嵌套的字典。而dst字典不是嵌套的字典。

有什么简单的方法来进行此转换吗?


4
一种简单的递归版本可能足够,但在嵌套层级较大时效率不高。你已经尝试了什么?有一个起点通常会更好地帮助你。 - Netwave
3个回答

6
这是一个用于压平字典的Python包。你可以使用它。 https://pypi.org/project/flatten-dict/ 实现:
from flatten_dict import flatten

nested = {'a': {'b': 1, 'c': {'d': 2}},
          'b': 3,
          'c': {'d': 4, 'a': 5}}

flat = flatten(nested, reducer=lambda k1, k2: k2 if k1 is None else k1 + '.' + k2)
print(flat)
# {'a.b': 1, 'a.c.d': 2, 'b': 3, 'c.d': 4, 'c.a': 5}

如果字典中包含列表,例如 {'b': [{'c':{'d':[1,2,3]}}]},则 flatten_dict 将无法工作。 - ybdesire
在这种情况下,您期望什么输出? {'b.c.d': [1,2,3]} ?? - Jyoti Arora
{'b': [{'c':{'d':[1,2,3]}}, 'e':[{'f':3}, {'g':6}]]} 转换为 {'b.c.d':[1,2,3], 'b.e.f':3, 'b.e.g':6}。 - ybdesire
应该像这样吗? {'b': [{'c':{'d':[1,2,3]}}, 'e':[{'f':3}, {'g':6}]]} . ---> . {'b_0.c.d':[1,2,3], ''b_1.e_0.f":3, b_1.e_1.g:6}因为列表中可能包含重复的字典,所以在这种情况下是否也需要索引? - Jyoti Arora
如果值是列表,Flatten_dict 不会将其扁平化。 - Jyoti Arora
嵌套 = {'b': [{'c':{'d':[1,2,3]}}], 'e' : [{'f':3}, {'g':6}]}从flatten_json导入平铺打印(flatten(nested,“。”)) - Jyoti Arora

2
有多种方法,这是其中一种方法。
最初的回答:

有多种方法。以下是其中一种方法。

    nested_dict = {
        'a': {
            'b': 1,
            'c': {
                'd': 2
            }
        },
        'b': 3,
        'c': {
            'd': 4,
            'a': 5
        },
    }

    flatten_dict = {}

    def flatten_the_nested(nested_dict, parent_key=''):
        for key, value in nested_dict.items():
            new_key = parent_key + '.' + key if parent_key is not '' else key
            if isinstance(value, dict):
                flatten_the_nested(value, new_key)
            else:
                flatten_dict[new_key] = value

        return flatten_dict


     print(flatten_the_nested(nested_dict, ''))

你将会得到以下结果。最初的回答。
    {'c.d': 4, 'c.a': 5, 'b': 3, 'a.b': 1, 'a.c.d': 2}

如果您想使用一些库,那么可以使用https://pypi.org/project/flatten-dict/。"最初的回答"

0

嗯,这并不复杂。只需要几分钟我就得到了以下内容:

def flatten(dic, prefix = ""):
    if prefix is not "":
        prefix = prefix + "."

    result = {}

    for k, v in dic.iteritems():
        if isinstance(v, dict):
            for k1, v1 in flatten(v, prefix + k).iteritems():
                result[k1] = v1
        else:
            result[prefix + k] = v

    return result

我还没有彻底测试过这个算法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接