我正在 Python 中使用一些深度嵌套的字典,并尝试递归地更新它们。数据(代表一棵二叉树)看起来像这样:
{
"root": {
"value": 1.0,
"name": null,
"children": [
{
"value": 0.5,
"name": null,
"children": [
{
"value": 0.5,
"name": null,
"children": []
},
{
"value": 3.0,
"name": "B",
"children": []
}
]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
字典的每一层都有一个值和名称键。我希望这两个键的每个实例(即每个级别)都成为附加子字典的值(例如,名称为“text”),即数据需要如下所示:
{
"root": {
"text": {
"value": 1.0,
"name": null,
},
"children": [
{
"text": {
"value": 0.5,
"name": null,
},
"children": [
{
"text": {
"value": 0.5,
"name": null,
},
"children": []
},
{
"text": {
"value": …Run Code Online (Sandbox Code Playgroud)