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

Python嵌套字典列表指定字段更新方法及内置库咨询

嵌套字典列表的字段更新解决方案

问题背景

给定一个嵌套结构的字典列表test,需要将其中特定位置的requires字段对应的string[]值更新为["aaaa", "bbb"]。由于结构嵌套较深,且存在重复键名,无法仅通过字段名直接定位,目前已筛选出包含order键的项,但不确定如何完成后续更新。

原始数据结构如下:

test = [
{
    "key": "prog",
    "value": {
        "instance[]": [
            {
                "key": "name",
                "value": {
                    "string": "NAME"
                },
                "verif": 22222222
            },
            {
                "key": "user",
                "value": {
                    "string": "AAAAA"
                },
                "verif": 22222222
            }
        ]
    },
    "verif": 22222222
},
{
    "key": "sytem_platform",
    "value": {
        "bool": "false"
    },
    "verif": 22222222
},
{
    "key": "system_beh",
    "value": {
        "bool": "true"
    },
    "verif": 22222222
},
{
    "key": "check_beh",
    "value": {
        "bool": "true"
    },
    "verif": 22222222
},
{
    "key": "order",
    "value": {
        "instance[]": [
            {
                "key": "order_det",
                "value": {
                    "instance[]": [
                        {
                            "key": "requires",
                            "value": {
                                "string[]": []
                            },
                            "verif": 22222222
                        },
                        {
                            "key": "status",
                            "value": {
                                "string[]": []
                            },
                            "verif": 22222222
                        },
                        {
                            "key": "system_status",
                            "value": {
                                "string[]": [
                                    "sys1.out",
                                    "sys1.checking"
                                ]
                            },
                            "verif": 22222222
                        },
                        {
                            "key": "weather_status",
                            "value": {
                                "instance[]": [
                                    {
                                        "key": "humdiity",
                                        "value": {
                                            "double": 5.0
                                        },
                                        "verif": 22222222
                                    },
                                    {
                                        "key": "temp",
                                        "value": {
                                            "double": 70.0
                                        },
                                        "verif": 22222222
                                    }
                                ]
                            },
                            "verif": 22222222
                        },
                        {
                            "key": "environment",
                            "value": {
                                "string[]": []
                            },
                            "verif": 22222222
                        }
                    ]
                },
                "verif": 22222222
            }
        ]
    },
    "verif": 22222222
}
]

需要更新的目标部分:

{
    "key": "requires",
    "value": {
        "string[]": ["aaaa", "bbb"]
    },
    "verif": 22222222
}

解决方案

Python标准库中没有专门针对这种嵌套结构字段更新的工具,但可以通过基础的字典、列表操作完成,以下提供两种可行方法:

方法一:固定结构直接索引更新

如果数据结构固定,可通过链式索引直接定位目标字段并更新:

# 定位到order对应的字典项
order_item = next(item for item in test if item["key"] == "order")
# 深入到order_det下的instance列表
order_det_instances = order_item["value"]["instance[]"][0]["value"]["instance[]"]
# 找到requires对应的项
requires_item = next(item for item in order_det_instances if item["key"] == "requires")
# 更新string[]的值
requires_item["value"]["string[]"] = ["aaaa", "bbb"]

由于字典和列表是引用类型,上述修改会直接作用于原始的test列表。

方法二:递归查找通用更新

如果数据结构可能变化,可编写递归函数遍历嵌套结构,定位目标字段并更新:

def update_target_field(data, target_key, new_value):
    if isinstance(data, list):
        for item in data:
            update_target_field(item, target_key, new_value)
    elif isinstance(data, dict):
        # 匹配到目标key时更新对应值
        if data.get("key") == target_key:
            data["value"]["string[]"] = new_value
        # 递归遍历所有子节点
        for value in data.values():
            update_target_field(value, target_key, new_value)

# 调用函数更新requires字段
update_target_field(test, "requires", ["aaaa", "bbb"])

此方法无需依赖固定结构,会遍历所有嵌套层级,找到所有key为requires的项并更新其string[]值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 13:09:52