【发布时间】:2020-03-15 05:45:45
【问题描述】:
寻找一个 通用 解决方案,我可以在其中从 dict 中删除特定键及其值。 例如,如果 dict 包含以下嵌套键值对:
data={
"set": {
"type": "object", #<-- should remove this key:value pair
"properties": {
"action": {
"type": "string", #<-- should NOT remove this key:value pair
"description": "My settings"
},
"settings": {
"type": "object", #<-- should remove this key:value pair
"description": "for settings",
"properties": {
"temperature": {
"type": "object", #<-- should remove this key:value pair
"description": "temperature in degree C",
"properties": {
"heater": {
"type": "object", #<-- should remove this key:value pair
"properties": {
"setpoint": {
"type": "number"
},
},
"additionalProperties": false
},
},
"additionalProperties": false
},
},
"additionalProperties": false
}
},
"additionalProperties": false
}
}
我想要一个没有"type":"object" 的输出 dict 跨越这个键:值对的出现。
预期的输出应该产生没有"type":"object"的结果
【问题讨论】:
-
要删除字典的元素,您可以查看https://stackoverflow.com/questions/5844672/delete-an-element-from-a-dictionary 对于您的典型问题,您可以使用递归函数
-
这是一个通用的递归字典搜索stackoverflow.com/questions/14962485/…
标签: python dictionary recursion nested