Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归地找到路径&;值在列表中的位置_Python_Json_Dictionary_Recursion_Arraylist - Fatal编程技术网

Python 递归地找到路径&;值在列表中的位置

Python 递归地找到路径&;值在列表中的位置,python,json,dictionary,recursion,arraylist,Python,Json,Dictionary,Recursion,Arraylist,我正在使用的解析大型嵌套JSON文件(有时文件大小大于25MB)。我在JSON文件的列表中查找值的位置时遇到问题 以下是JSON文件的简化小示例: parsed = { "type": "Program", "body": [ { }, { "type": "ExpressionStatement", "expression": { "type": "C

我正在使用的解析大型嵌套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: 而预期结果如下

[
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
list
if-isinstance(dic,list):
和dic以及
elif-isinstance(value,list)和value:

您的预期输出是什么?@MohitMotwani[1,['expression','callee','property','0','a','1','property','1','expression','callee','property','0','a','1','property','1','expression','callee','property','0','a','1','name','name',1,['expression','callee','property',4','name']在你的问题中贴出来,太不清楚了,无法在评论中阅读。@MohitMotwani完成了。谢谢你提到它。你的输出中的1、0、4和4是什么?
[
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']
]