如何从嵌套字典的每个条目中提取值?

时间:2019-07-26 18:04:26

标签: python python-3.x dictionary indexing

我有一个解析Maven依赖树并按需要组织数据的程序。我不确定如何正确索引数据以返回它。这是从Maven依赖树解析器输出的数据的摘要(使用pprint)。

[{'name': 'org.antlr'},
 {'type': 'dependency'},
 {'metadata': [{'groupId': 'org.antlr'},
               {'artifactId': 'antlr4',
                'children': [({'name': 'org.antlr'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'org.antlr'},
                                            {'artifactId': 'antlr4-runtime'}]}),
                             ({'name': 'org.antlr'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'org.antlr'},
                                            {'artifactId': 'antlr-runtime'}]}),
                             ({'name': 'org.antlr'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'org.antlr'},
                                            {'artifactId': 'ST4'}]}),
                             ({'name': 'org.abego.treelayout'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'org.abego.treelayout'},
                                            {'artifactId': 'org.abego.treelayout.core'}]}),
                             ({'name': 'org.glassfish'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'org.glassfish'},
                                            {'artifactId': 'javax.json'}]}),
                             ({'name': 'com.ibm.icu'},
                              {'type': 'dependency'},
                              {'metadata': [{'groupId': 'com.ibm.icu'},
                                            {'artifactId': 'icu4j'}]})]}]}]

我尝试过:

pprint([name for name in parsedTree[0]])

pprint(parsedTree[0:1])

或其中的变体,其中parsedTree是上面显示的输出。

最终,我只想使用递归或生成器仅返回每个名称,以便为API调用准备此信息。我不知道如何单独索引和提取名称(没有元数据或类型)。有没有办法做到这一点? 先感谢您。 我想分别获取每个名称,以便如果我执行Beans.name之类的操作,它将返回

org.antlr
org.antlr
org.antlr
org.antlr
org.antlr
org.abego.treelayout
org.glassfish
com.ibm.icu

2 个答案:

答案 0 :(得分:2)

我以前的回答是错误的,我认为这应该起作用:

def get_all_names(parsed_tree, li):
    if not isinstance(parsed_tree, list) and not isinstance(parsed_tree, tuple):
        return li
    def get_all_names_dict(dictionary, li):
        if not isinstance(dictionary, dict):
            get_all_names(dictionary, li)
            return
        for key in dictionary.keys():
            if key == 'name':
                li.append(dictionary[key])
            elif isinstance(dictionary[key], dict):
                get_all_names_dict(dictionary[key], li)
            elif isinstance(dictionary[key], list):
                get_all_names(dictionary[key], li)
            elif isinstance(dictionary[key], tuple):
                get_all_names(dictionary[key], tuple)
    for val in parsed_tree:
        get_all_names_dict(val, li)
    return li

print('\n'.join(get_all_names(parsedTree, [])))

对不起,等待中!

答案 1 :(得分:0)

您只需要学习字典,元组和字典,即可使用关键字“名称”搜索字典。如果您希望在这里使用其他类型,请将它们包括在相关的“实例”检查中。


def get_nested_name(tree):
    names = []
    if isinstance(tree, (list, tuple)):
        for branch in tree:
            names.extend(get_nested_name(branch))

    elif isinstance(tree, dict):
        try:
            names.append(tree['name'])
        except KeyError:
            # this dict dont have a "name" key, lets ignore it
            pass

        branches = filter(lambda x: isinstance(x, (list, dict, tuple)), tree.values())
        for b in branches:
            names.extend(get_nested_name(b))

    return names