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

如何从嵌套JSON中提取值生成独立的Python字典

嵌套JSON转指定结构Python字典解决方案

转换代码

直接使用字典推导式即可完成转换,代码如下:

# 原始嵌套数据
data = {
    "0": {
        "0": "Accomplishments:",
        "1": "Testing"
    },
    "1": {
        "0": "Priorities:",
        "1": "Priorities value"
    },
    "2": {
        "0": "agenda:",
        "1": "3 months"
    }
}

# 核心转换逻辑:遍历外层字典的所有值,取每个子字典的"0"字段为键,"1"字段为值
target_dict = {sub_dict["0"]: sub_dict["1"] for sub_dict in data.values()}

print(target_dict)

运行结果

{
    "Accomplishments:": "Testing",
    "Priorities:": "Priorities value",
    "agenda:": "3 months"
}

注:你给出的期望输出中Priorities:对应的值误写为Testing,上述代码会严格按照原始数据匹配值,若需要手动替换该值可自行添加判断逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 23:54:04