在嵌套循环中将键/值添加到Python Dict时出错

时间:2019-05-22 01:20:36

标签: python loops dictionary

我的Json结构如下:

 {
   "_id" : ObjectId("asdasda156121s"),
    "Hp" : {
    "bermud" : [ 
        {
            "abc" : {
                "gfh" : 1,
                "fgh" : 0.0,
                "xyz" : [ 
                    {
                        "kjl" : "0",
                        "bnv" : 0,
                    }
                ],
                "xvc" : "bv",
                "hgth" : "INnn",
                "sdf" : 0,
                }
            }
        },
       {
            "abc" : {
                "gfh" : 1,
                "fgh" : 0.0,
                "xyz" : [ 
                    {
                        "kjl" : "0",
                        "bnv" : 0,
                    }
                ],
                "xvc" : "bv",
                "hgth" : "INnn",
                "sdf" : 0,
                }
            }
        },
        ..

我正在尝试解析此json并在对象'xyz'内使用键['cat']添加新值,以下是我的py代码。

data = []

for x in a:
    for y in x['Hp'].values():
        for z in y:
            for k in z['abc']['xyz']:
                for m in data:
                    det = m['response']
                    // Some processing with det whose output is stored in s 
                    k['cat'] = s
    print x

但是,当打印x时,仅将最后一个值附加到整个字典中,而s则具有不同的值。很明显,每次循环时,“ cat”键都会被覆盖,但找不到正确的方法。我在犯什么错误?

0 个答案:

没有答案