As I understand it, you want to recursively search for a key in a nested dict
and promote its value.
This might not be super efficient, but it should work. It also does not really explore dictionaries with lists as values but your example data does not have them so I did not implement that.
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))
Should give you:
{
"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"
}
}
}
}
Note that I added an addition level of nesting with a sub-nested key match just to show that scenario. Additional optimizations like support for lists and avoiding updates to unchanged keys available for a reasonable fee :-P
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…