7

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

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 嵌套的字典,其中每个值要么是另一个字典,要么是一个包含单个项目的列表。

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

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)

我怀疑这只是递归问题,但任何建议将不胜感激。

谢谢!

4

2 回答 2

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
于 2012-06-22T23:53:48.470 回答
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
于 2012-06-23T00:46:11.073 回答