如果我有以下 python dict“mydict”:
mydict = {
"folder": "myfolder",
"files": [
{ "folder": "subfolder",
"files": []
},
{ "folder": "anotherfolder",
"files": [
{ "folder": "subsubfolder",
"files": []
},
{ "folder": "subotherfolder",
"files": []
}
]
},
]
}
如果我有另一个字典“newdict”,我怎样才能做到这一点:
newdict = {
"folder":"newfolder"
"files":[]
}
如何/是否可以编写一个带有两个参数的函数(“要添加到的当前字典”、“我想添加的字典”、“文件夹名到插入下”,这样在调用函数后:
desiredfunction(mydict, newdict, "subsubfolder")
我希望该函数在我现有的“mydict”中搜索适当的“files”数组以附加“newdict”。
mydict = {
"folder": "myfolder",
"files": [
{ "folder": "subfolder",
"files": []
},
{ "folder": "anotherfolder",
"files": [
{ "folder": "subsubfolder",
"files": [
{
"folder":"newfolder"
"files":[]
}
]
},
{ "folder": "subotherfolder",
"files": []
}
]
},
]
}
做这个的最好方式是什么?我不知道如何/是否可以搜索现有的字典结构,在嵌套字典的多个级别中搜索以适当地插入新的字典,感谢帮助。