无论所选键出现在深度嵌套的字典中,我如何用它的值替换 key:value 对?

How can I replace a key:value pair by its value wherever the chosen key occurs in a deeply nested dictionary?

其他问题

这是 的衍生产品,其中答案仅适用于一次性嵌套字典。 它是 Loop through all nested dictionary values? 的衍生产品,我无法解决这个问题。

之前:

我有一个嵌套了很多次的字典。

dict_nested = {
    "key_":{
        "key0a":{
            "key1a":{
                "sub_key2a":"sub_value2a",
                "sub_key2b":"sub_value2b"},
            "key1b":"value1b"},
        "key0b":{
            "key_XYZ":{
                "key1a":{
                    "sub_key2a":"sub_value2a",
                    "sub_key2b":"sub_value2b"},
                "key1b":"value1b"}
            }
        }
    }

之后:

结果应如下所示:

dict_nested_new = {
    "key_":{
        "key0a":{
            "sub_key2a":"sub_value2a",
            "sub_key2b":"sub_value2b",
            "key1b":"value1b"},
        "key0b":{
            "key_XYZ":{
                "sub_key2a":"sub_value2a",
                "sub_key2b":"sub_value2b",
                "key1b":"value1b"}
            }
        }
    }

在迭代时修改 Python 字典

当我循环遍历字典的项目以删除/替换时,出现错误

RuntimeError: dictionary changed size during iteration

这需要以某种方式避免。

每次出现在字典某处时,如何用它的值替换 "key1a":SOME_VALUE 键值对?

据我了解,您想在嵌套 dict 中递归搜索键并提升其值。

这可能效率不高,但应该可以。它也没有真正探索以列表作为值的字典,但是您的示例数据没有它们,所以我没有实现它。

import copy
import json

def find_replace(this_dict, target_key):
    ## optional depending on if you care that you mutate this_dict
    this_dict = copy.deepcopy(this_dict)

    for key in this_dict:
        # if the current value is a dict, dive into it
        if isinstance(this_dict[key], dict):
            this_dict[key] = find_replace(this_dict[key], target_key)

        # if the current key is our target merge the values and remove the key
        if key == target_key:
            this_dict = {**this_dict, **this_dict[key]}
            del this_dict[key]

    return this_dict

dict_nested = {
    "key_":{
        "key0a":{
            "key1a":{
                "sub_key2a":"sub_value2a", 
                "sub_key2b":"sub_value2b"
            }, 
            "key1b":"value1b"
        },
        "key0b":{
            "key_XYZ":{
                "key1a":{
                    "sub_key2a":"sub_value2a", 
                    "sub_key2b":"sub_value2b",
                    "key1a": {
                        "sub_key3a":"sub_value3a", 
                        "sub_key3b":"sub_value3b"
                    }, 
                }, 
                "key1b":"value1b"
            }
        }
    }
}

dict_nested_new = find_replace(dict_nested, "key1a")
print(json.dumps(dict_nested_new, indent=4))

应该给你:

{
    "key_": {
        "key0a": {
            "key1b": "value1b",
            "sub_key2a": "sub_value2a",
            "sub_key2b": "sub_value2b"
        },
        "key0b": {
            "key_XYZ": {
                "key1b": "value1b",
                "sub_key2a": "sub_value2a",
                "sub_key2b": "sub_value2b",
                "sub_key3a": "sub_value3a",
                "sub_key3b": "sub_value3b"
            }
        }
    }
}

请注意,我添加了一个带有子嵌套键匹配的额外嵌套级别只是为了展示该场景。额外的优化,例如支持列表和避免以合理的费用更新未更改的键:-P