0

我在 python 2.7 中有一个非常棘手的问题,有一个列表和嵌套的字典。

我有一个这样的列表:

    [
"modeA", 
{
    "alpha": {
        "mainFF": {
            "blocType": "f000", 
            "baseAddress": "]254:0x0]" 

        }
    }
}, 
"modeA", 
{
    "alpha": {
        "sideM": {
            "blocType": "smmm", 
            "baseAddress": "]0x100:0x0]" 

        }, 
        "observer_main_ErrorLogger_0": {
            "blocType": "assa", 
            "baseAddress": "]0x180:0x100]" 

        }
    }
}, 
"modeA", 
{
    "beta": {
        "mainFF": {
            "blocType": "Firewall", 
            "baseAddress": "]0x1000:0x0]" 

        }
    }
}, 
"modeB", 
{
    "beta": {
        "sideM": {
            "blocType": "SidebandManager", 
            "baseAddress": "]0x100:0x0]" 

        }, 
        "observer_main_ErrorLogger_0": {
            "blocType": "ErrorLogger_0", 
            "baseAddress": "]0x180:0x100]"

        }
    }
}

]

它实际上是 JSON 格式以提高可见性,但在将其转换为 JSON 之前,它是一个包含多个字典的列表以及一些字符串,例如modeAmodeB

我想按这个(大!)列表排序mode。并将此列表转换为 100% dict => somodeA并将modeB成为新 dict 输出中的键:

{
"modeA":{
    "alpha": {
        "mainFF": {
            "blocType": "f000", 
            "baseAddress": "]254:0x0]" 

        },
        "sideM": {
            "blocType": "smmm", 
            "baseAddress": "]0x100:0x0]" 

        }, 
        "observer_main_ErrorLogger_0": {
            "blocType": "assa", 
            "baseAddress": "]0x180:0x100]" 

        }
    },

    "beta": {
        "mainFF": {
            "blocType": "Firewall", 
            "baseAddress": "]0x1000:0x0]" 

        }
    }
},

"modeB":{
    "beta": {
        "sideM": {
            "blocType": "SidebandManager", 
            "baseAddress": "]0x100:0x0]" 

        }, 
        "observer_main_ErrorLogger_0": {
            "blocType": "ErrorLogger_0", 
            "baseAddress": "]0x180:0x100]" 

        }
    }
}

}

我的策略是将原始列表拆分为列表的偶数元素(即:模式类型)到另一个子列表,然后迭代并尝试对嵌套的字典进行排序。

如果有人可以帮助我,我将非常感激!

4

0 回答 0