我有一个看起来像这样的字典:
d = {'Food': {'Fruit' : {'Apples' : {'Golden Del.' : ['Yellow'],'Granny Smith' : ['Green'],'Fuji' : ['Red'],},'Cherries' : ['Red'],'Bananas' : ['Yellow'],'Grapes' : {'Red Grapes' : ['Red'],'Green Grapes' : ['Green'],'Dessert': {'Baked Ds' : {'Cakes' : {'Yellow Cake' : ['Yellow'],'Red Velvet' : ['Red'],'Cookies' : ['Yellow'],'Steak' : ['Red'],'Other': ['Blue'],}
所以基本上是一个n嵌套的dict,其中每个值都是另一个dict或包含单个项的列表.
假设我想通过单个列表项过滤它,例如“Red”,结果将是:
d = {'Food': {'Fruit' : {'Apples' : {'Fuji' : ['Red'],'Grapes' : {'Red Grapes' : ['Red'],'Dessert': {'Baked Ds' : {'Cakes' : {'Red Velvet' : ['Red'],}
因此结构保持不变,但是除了列表项之外没有“红色”的所有内容都被删除,一直到层次结构.
有什么建议?我已经搞砸了一段时间并想出了这个,但它似乎不起作用:
def filterNestedDict(node,searchItem):
if isinstance(node,list):
return node
else:
for key,value in node.iteritems():
if isinstance(value,dict) and value is not {}:
return {key: filterNestedDict(value,searchItem)}
elif searchItem in value:
return {key: filterNestedDict(value,searchItem)}
return filterNestedDict(bigTree,searchItem)
我怀疑这只是一个递归问题,但任何建议都会非常感激.
谢谢!
最佳答案
你非常接近,这应该为你做的伎俩:
def filter_nested_dict(node,search_term):
if isinstance(node,list):
if node[0] == search_term:
return node
else:
return None
else:
dupe_node = {}
for key,val in node.iteritems():
cur_node = filter_nested_dict(val,search_term)
if cur_node:
dupe_node[key] = cur_node
return dupe_node or None