Python:如何通过叶值过滤n-nested dicts的dict?

时间:2012-06-22 23:25:59

标签: python dictionary nested

我有一个看起来像这样的字典:

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-nested dict,其中每个值都是另一个dict或包含单个项目的列表。

假设我想通过单个列表项过滤此项,例如“红色”,结果将是:

d = {'Food': {'Fruit'  : {'Apples'    : {'Fuji'        : ['Red'],    
                                        },
                          'Cherries'  : ['Red'],
                          'Grapes'    : {'Red Grapes'  : ['Red'], 
                                        },
                          },
              'Dessert': {'Baked Ds' : {'Cakes'        : {'Red Velvet'  : ['Red'],
                                                          },
                                        },
                          },
              'Steak'  : ['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)

我怀疑这只是一个递归问题,但任何建议都会非常感激。

谢谢!

2 个答案:

答案 0 :(得分:4)

你非常接近,这应该适合你:

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

答案 1 :(得分:0)

from collections import Mapping


def yield_values_2(d):
  for key in d.keys():
    if isinstance(d[key], Mapping):
      yield_values_2(d[key]) 
      if not d[key]:
        del d[key]
    elif d[key] != ["Red"]:
      del d[key]

d = {'Food': {'Fruit'  : {'Apples'    : {'Fuji'        : ['Red'], 'Fuji2': ['Blue']    
                                        },
                                                                  'Cherries'  : ['Red'],
                                                                                            'Grapes'    : {'Red Grapes'  : ['Red'], 
                                                                                                                                    },
                                                                                                                                                              },
                                                                                                                                                                            'Dessert': {'Baked Ds' : {'Cakes'        : {'Red Velvet'  : ['Red'],
                                                                                                                                                                                                                                      },
                                                                                                                                                                                                                                                                              },
                                                                                                                                                                                                                                                                                                        },
                                                                                                                                                                                                                                                                                                                      'Steak'  : ['Red'],
                                                                                                                                                                                                                                                                                                                                   },
                                                                                                                                                                                                                                                                                                                                       }

print d
yield_values_2(d)
print d