小编Nit*_*ish的帖子

深度Python字典递归

我有一个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": {}}},
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

我想将其更改为类似这样的内容(如果值为空{},则将键作为其父项的值)

d = {
    "config": {
        "application": {
            "payment": {
                "dev": {
                    "modes": "credit,debit,emi",
                    "company": {
                        "address": {
                            "city": "London", 
                            "pincode": "LD568162"
                        },
                        "country": "United Kingdom",
                        "phone": "7865432765"
                    },
                    "levels": "0,1,2" …
Run Code Online (Sandbox Code Playgroud)

python tree recursion recursive-datastructures data-structures

5
推荐指数
1
解决办法
84
查看次数