在列表中递归查找值的路径和位置

时间:2018-08-10 09:30:39

标签: python json dictionary recursion arraylist

我正在使用ESPRIMAlibrary for Python解析巨大的嵌套JSON文件(有时文件大小大于25MB)。在JSON文件的列表中查找值的位置时遇到问题。

这是JSON文件的简化示例:

parsed = {
    "type": "Program",
    "body": [
        {
        },
        {
            "type": "ExpressionStatement",
            "expression": {
                "type": "CallExpression",
                "callee": {
                    "type": "MemberExpression",
                    "computed": "false",
                    "object": {
                        "type": "Identifier",
                        "name": "localStorage"
                    },
                    "property": [
                        {
                            "a": [
                                {
                                    "type": "Identifier"
                                },
                                {
                                    "name": "getItem",
                                    "property": [
                                        {
                                            "b": [
                                                {
                                                    "name": "getItem"
                                                },
                                                {
                                                    "name": "getItem"
                                                }
                                            ]
                                        }
                                    ]
                                }
                            ]
                        },
                        {
                            "type": "Identifier",
                            "name": "getItem"
                        },
                        {
                            "type": "Identifier"
                        },
                        {
                            "type": "Identifier",
                            "name": "abcd"
                        },
                        {
                            "type": "Identifier",
                            "name": "getItem"
                        }
                    ]
                },
                "arguments": [
                    {
                        "type": "Literal",
                        "value": "myCat0",
                        "raw": "'myCat0'"
                    }
                ]
            }
        },
    ],
    "sourceType": "script"
}

我使用的功能如下:

result_k = []
path_k = []
x = z = 0

def get_keys(dic, target):
    global x, z
    try:
        if isinstance(dic, list) and dic:
            for d in dic:
                get_keys(d, target)
                z += 1
        else:
            for key, value in dic.items():
                path_k.append(key)
                if isinstance(value, list) and not value:
                    pass
                elif isinstance(value, list) and value:
                    for v in value:
                        path_k.append(x)
                        get_keys(v, target)
                        x += 1
                        path_k.pop()
                    x = 0
                elif isinstance(value, dict):
                    get_keys(value, target)
                if value == target:
                    result_k.append(z)
                    result_k.append(copy(path_k))
                path_k.pop()
    except Exception as err:
        print('\x1b[1;37;41m{}\x1b[0m'.format(str(err)))
        traceback.print_exc()

,这是关于后一级列表的输出错误;两个不正确的位置都会在结果中突出显示(在 b 之后):

[
1, ['expression', 'callee', 'property', 0, 'a', 1, 'property', 1, 'b', **1**, 'name'], 
1, ['expression', 'callee', 'property', 0, 'a', 1, 'property', 1, 'b', **2**, 'name'], 
1, ['expression', 'callee', 'property', 0, 'a', 1, 'name'], 
1, ['expression', 'callee', 'property', 1, 'name'], 
1, ['expression', 'callee', 'property', 4, 'name']
]

更新1: 而预期结果如下。

[
1, ['expression', 'callee', 'property', 0, 'a', 1, 'property', 1, 'b', **0**, 'name'], 
1, ['expression', 'callee', 'property', 0, 'a', 1, 'property', 1, 'b', **1**, 'name'], 
1, ['expression', 'callee', 'property', 0, 'a', 1, 'name'], 
1, ['expression', 'callee', 'property', 1, 'name'], 
1, ['expression', 'callee', 'property', 4, 'name']
]

我认为问题是由于重复检查列表dict和dic和list

中的列表是if isinstance(dic, list):还是elif isinstance(value, list) and value:而发生的

0 个答案:

没有答案