Python: How can I filter a n-nested dict of dicts by leaf value?

筅森魡賤 提交于 2019-12-07 00:15:20

问题


I've got a dict that looks something like this:

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'],
    }

So basically, an n-nested dict, where each value is either another dict or a list containing a single item.

Let's say that I want to filter this by a single list item, say, 'Red' such that the result would be:

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

So that the structure remains the same but everything that doesn't have 'Red' as its list item is removed, all the way up the hierarchy.

Any suggestions? I've messed around with this for a while and came up with this, but it doesn't seem to work:

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)

I suspect it's just an issue of recursion, but any suggestions would be greatly appreciated.

Thanks!


回答1:


You were pretty close, this should do the trick for you:

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



回答2:


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


来源:https://stackoverflow.com/questions/11165381/python-how-can-i-filter-a-n-nested-dict-of-dicts-by-leaf-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!