You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何提取Python嵌套字典中的键、一级及二级嵌套值

从嵌套字典提取指定字段并输出/存储

给定如下Python嵌套字典,需要打印(或赋值给变量)外层键、一级嵌套的name值、二级嵌套中status下的description值:

people = {
        "123": {
            "name": "Danny",
            "age": 100,
            "animal": "cat",
            "last_action": {
                "status": "Offline",
                "timestamp": 1664651202,
                "relative": "50 minutes ago"
            },
            "status": {
                "description": "Okay",
                "details": "",
                "state": "Okay",
                "color": "green",
                "until": 0
            },
            "number": "Six"
        },
        "456": {
            "name": "Suzy",
            "age": 42,
            "animal": "dog",  # 原代码此处漏加引号,已修正为字符串格式
            "last_action": {
                "status": "Offline",
                "timestamp": 1664636683,
                "relative": "4 hours ago"
            },
            "status": {
                "description": "Not Okay",
                "details": "",
                "state": "Okay",
                "color": "green",
                "until": 0
            },
            "number": "Twelve"
        },
        "789": {
            "name": "Chris",
            "age": 23,
            "animal": "horse",
            "last_action": {
                "status": "Offline",
                "timestamp": 1664636683,
                "relative": "4 hours ago"
            },
            "status": {
                "description": "Okay",
                "details": "",
                "state": "Okay",
                "color": "green",
                "until": 0
            },
            "number": "Two"
        }  
}

期望输出格式:

123 Danny Okay
456 Suzy Not Okay
789 Chris Okay

同时需要将这三个元素分别存为变量,而非仅输出整行内容。


实现方法

1. 遍历字典提取字段并打印

使用for循环遍历people.items(),它会返回每一组外层键(key)和对应的内层字典(value),然后从内层字典中提取所需字段:

for person_id, details in people.items():
    name = details["name"]
    status_desc = details["status"]["description"]
    # 打印结果
    print(f"{person_id} {name} {status_desc}")

运行这段代码后,就能得到你需要的输出格式。

2. 将字段存储为变量(或结构化数据)

如果需要把这些字段长期存储或后续复用,可以选择以下两种方式:

方式一:存储为列表元组

把每组字段打包成元组存入列表,方便统一管理和遍历:

person_info_list = []
for person_id, details in people.items():
    name = details["name"]
    status_desc = details["status"]["description"]
    person_info_list.append( (person_id, name, status_desc) )

# 后续使用示例
for info in person_info_list:
    pid, name, status = info
    print(f"ID: {pid}, 姓名: {name}, 状态: {status}")

方式二:循环内直接使用单个变量

如果仅需在循环过程中处理字段,可直接在循环内定义独立变量:

for person_id, details in people.items():
    current_id = person_id
    current_name = details["name"]
    current_status = details["status"]["description"]
    # 此处可添加针对这三个变量的业务逻辑
    print(f"正在处理用户:{current_name}(ID:{current_id}),状态:{current_status}")

内容的提问来源于stack exchange,提问作者Bruce Taylor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 00:55:54