深度 python 字典递归

Deep python dictionary recursion

我有一本 python 字典:

d = {
    "config": {
        "application": {
            "payment": {
                "dev": {
                    "modes": {"credit,debit,emi": {}},
                    "company": {
                        "address": {
                            "city": {"London": {}},
                            "pincode": {"LD568162": {}},
                        },
                        "country": {"United Kingdom": {}},
                        "phone": {"7865432765": {}},
                    },
                    "levels": {"0,1,2": {}},
                },
                "prod": {"modes": {"credit,debit": {}}, "levels": {"0,1": {}}},
            }
        }
    }
}

我想把它改成这样(如果值为空{}然后将键作为其父项的值)

d = {
    "config": {
        "application": {
            "payment": {
                "dev": {
                    "modes": "credit,debit,emi",
                    "company": {
                        "address": {
                            "city": "London", 
                            "pincode": "LD568162"
                        },
                        "country": "United Kingdom",
                        "phone": "7865432765"
                    },
                    "levels": "0,1,2"
                }, 
                "prod": {
                    "modes": "credit,debit",
                    "levels": "0,1"
                }
            }
        }
    }
 }

我尝试编写代码来遍历这个深层字典,但无法修改它以获得上述输出。请帮忙。

def recur(json_object):
    for x in list(json_object.items()):
        print(x)
        recur(json_object[x])

d={'config': {'application': {'payment': {'dev': {'modes': {'credit,debit,emi': {}}, 'company': {'address': {'city': {'London': {}}, 'pincode': {'LD568162': {}}}, 'country': {'United Kingdom': {}}, 'phone': {'7865432765': {}}}, 'levels': {'0,1,2': {}}}, 'prod': {'modes': {'credit,debit': {}}, 'levels': {'0,1': {}}}}}}}

解决方案 1

我们可以使用队列的非递归方法将文档的每个 inner/nested 元素排入队列,如果嵌套值只是 {}:

则将其作为值
# d = ...

queue = [d]

while queue:
    data = queue.pop()
    for key, value in data.items():
        if isinstance(value, dict) and list(value.values()) == [{}]:
            data[key] = list(value.keys())[0]
        elif isinstance(value, dict):
            queue.append(value)

print(d)

输出

{
    "config": {
        "application": {
            "payment": {
                "dev": {
                    "modes": "credit,debit,emi",
                    "company": {
                        "address": {
                            "city": "London",
                            "pincode": "LD568162"
                        },
                        "country": "United Kingdom",
                        "phone": "7865432765"
                    },
                    "levels": "0,1,2"
                },
                "prod": {
                    "modes": "credit,debit",
                    "levels": "0,1"
                }
            }
        }
    }
}

解决方案 2

这是一个递归方法

# d = ...

def recur(data):
    for key, value in data.items():
        if isinstance(value, dict) and list(value.values()) == [{}]:
            data[key] = list(value.keys())[0]
        elif isinstance(value, dict):
            recur(value)

recur(d)

print(d)

输出

  • 与解决方案 1 相同