python从嵌套字典中获取所有值

时间:2020-01-29 13:42:28

标签: python

我有以下命令,想访问所有ID:

"applicationCategories": [
        {
            "id": "test1",
            "name": "Email",
            "applications": [
                {
                    "id": "aaaaaaa",
                    "name": "Gmail"
                },
                {
                    "id": "bbbbb",
                    "name": "Host-based email (POP3/IMAP/SMTP)"
                },
            ]
        },
        {
            "id": "test2",
            "name": "Blogging",
            "applications": [
                {
                    "id": "ccccc",
                    "name": "Blogger"
                },
                {
                    "id": "dddd",
                    "name": "WordPress"
                }
            ] 

我想访问所有ID ...例如:

applicationCategories [“ id”] =给我test1,但我希望所有这些... test1和test2

3 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

d = {"applicationCategories": [
        {
            "id": "test1",
            "name": "Email",
            "applications": [
                {
                    "id": "aaaaaaa",
                    "name": "Gmail"
                },
                {
                    "id": "bbbbb",
                    "name": "Host-based email (POP3/IMAP/SMTP)"
                },
            ]
        },
        {
            "id": "test2",
            "name": "Blogging",
            "applications": [
                {
                    "id": "ccccc",
                    "name": "Blogger"
                },
                {
                    "id": "dddd",
                    "name": "WordPress"
                }
            ]
        }
    ]
}

ids = [x["id"] for x in d["applicationCategories"]]
print(ids)

输出:

['test1', 'test2']

或:

for i in d["applicationCategories"]:
    print(i["id"])

输出:

test1
test2

答案 1 :(得分:0)

您可以使用嵌套列表理解:

d = {'applicationCategories': [{'id': 'test1', 'name': 'Email', 'applications': [{'id': 'aaaaaaa', 'name': 'Gmail'}, {'id': 'bbbbb', 'name': 'Host-based email (POP3/IMAP/SMTP)'}]}, {'id': 'test2', 'name': 'Blogging', 'applications': [{'id': 'ccccc', 'name': 'Blogger'}, {'id': 'dddd', 'name': 'WordPress'}]}]}
result = [i for b in d['applicationCategories'] for i in [b['id'], *[k['id'] for k in b['applications']]]]

输出:

['test1', 'aaaaaaa', 'bbbbb', 'test2', 'ccccc', 'dddd']

但是,对于任意深度的结构,您可以将递归与生成器一起使用:

def get_ids(d):
   yield from filter(None, [d.get('id')])
   for b in d.values():
     if isinstance(b, dict):
       yield from get_ids(b)
     elif isinstance(b, list):
       for j in b:
          yield from get_ids(j)

list(get_ids(struct))

输出:

['test1', 'aaaaaaa', 'bbbbb', 'test2', 'ccccc', 'dddd']

答案 2 :(得分:0)

让我们假设您提供的字典被引用为my_dictionary。在这种情况下:

my_dictionary = {"applicationCategories": [
        {
            "id": "test1",
            "name": "Email",
            "applications": [
                {
                    "id": "aaaaaaa",
                    "name": "Gmail"
                },
                {
                    "id": "bbbbb",
                    "name": "Host-based email (POP3/IMAP/SMTP)"
                },
            ]
        },
        {
            "id": "test2",
            "name": "Blogging",
            "applications": [
                {
                    "id": "ccccc",
                    "name": "Blogger"
                },
                {
                    "id": "dddd",
                    "name": "WordPress"
                }
            ]}]} 

for elem in my_dictionary["applicationCategories"]:
        print(elem["id"])

此代码将打印test1和test2,因此允许您访问这些值。但是,您的问题很模糊,我不确定您要做什么。

applicationCategories [“ id”] =给我test1

实际上不是。