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

标签 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

最佳答案

假设您提供的字典被引用为 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"] = gives me test1

确实没有。

关于python 从嵌套字典中获取所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59968375/

相关文章:

python - 如何将 key 输入终端以进行单元测试

python - 检查进程是否仅使用 Python 内置模块在 Windows 中运行

python - “类型”对象在函数定义时不可订阅

python - Psychopy:是否可以在同一帧内显示2个不同颜色的单词?

python - PyQt QTextEdit : Detect change Event

python - 我可以从 OpenERP 的 TreeView 中触发窗口操作吗?

python 查找具有共享元素的集合

python - wxPython在Ubuntu中打开文件时出错

python - 使用 django-storages/boto 在本地收集静态作品,但在 Heroku 上没有

Python 用于识别样本中的最小染色体区域