Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
查找嵌套python dict中出现的所有键_Python_Json_Dataframe_Dictionary - Fatal编程技术网

查找嵌套python dict中出现的所有键

查找嵌套python dict中出现的所有键,python,json,dataframe,dictionary,Python,Json,Dataframe,Dictionary,我有一本这样的字典: a = {'compatibility': {'schema': ['attribute_variables/evar44', 'event42', 'container_visitors'], 'status': 'valid', 'supported_features': ['function_and', 'function_attr', 'function_container', 'function_event', 'fun

我有一本这样的字典:

a = {'compatibility': {'schema': ['attribute_variables/evar44',
   'event42',
   'container_visitors'],
  'status': 'valid',
  'supported_features': ['function_and',
   'function_attr',
   'function_container',
   'function_event',
   'function_event-exists',
   'function_streq'],
  'supported_products': ['o', 'data_warehouse', 'discover'],
  'supported_schema': ['warehouse', 'n'],
  'validator_version': '1.1.11'},
 'definition': {'container': {'context': 'visitors',
   'func': 'container',
   'pred': {'func': 'and',
    'preds': [{'description': 'e42',
      'evt': {'func': 'event', 'name': 'metrics/event42'},
      'func': 'event-exists'},
     {'description': 'v44',
      'func': 'streq',
      'str': '544',
      'val': {'func': 'attr', 'name': 'variables/evar44'}}]}},
  'func': 'segment',
  'version': [1, 0, 0]},
 'description': '',
 'id': 's2165c30c946ebceb',
 'modified': '12',
 'name': 'Apop',
 'owner': {'id': 84699, 'login': 'max', 'name': 'Max'},
 'reportSuiteName': 'App',
 'rsid': 'test',
 'siteTitle': 'App',
 'tags': []}
我想提取每个键“description”、“func”和“str”/“num”的值,并在这些dict的一个数据帧中返回这些值

我用这段代码进行了尝试,但我无法获得每个值并将这些值放在一个数据帧中

def findkeys(node, kv):
    if isinstance(node, list):
        for i in node:
            for x in findkeys(i, kv):
               yield x
    elif isinstance(node, dict):
        if kv in node:
            yield node[kv]
        for j in node.values():
            for x in findkeys(j, kv):
                yield x
对于我的示例,我希望有以下输出:

pd.DataFrame(np.array([['e42', 'event', 'NaN'], ['v44', 'streq', '544']]), 
               columns=['description', 'funk', 'str/num'])

下面的代码将“有趣”键的值收集到dict中

from collections import defaultdict

a = {'compatibility': {'schema': ['attribute_variables/evar44',
                                  'event42',
                                  'container_visitors'],
                       'status': 'valid',
                       'supported_features': ['function_and',
                                              'function_attr',
                                              'function_container',
                                              'function_event',
                                              'function_event-exists',
                                              'function_streq'],
                       'supported_products': ['o', 'data_warehouse', 'discover'],
                       'supported_schema': ['warehouse', 'n'],
                       'validator_version': '1.1.11'},
     'definition': {'container': {'context': 'visitors',
                                  'func': 'container',
                                  'pred': {'func': 'and',
                                           'preds': [{'description': 'e42',
                                                      'evt': {'func': 'event', 'name': 'metrics/event42'},
                                                      'func': 'event-exists'},
                                                     {'description': 'v44',
                                                      'func': 'streq',
                                                      'str': '544',
                                                      'val': {'func': 'attr', 'name': 'variables/evar44'}}]}},
                    'func': 'segment',
                    'version': [1, 0, 0]},
     'description': '',
     'id': 's2165c30c946ebceb',
     'modified': '12',
     'name': 'Apop',
     'owner': {'id': 84699, 'login': 'max', 'name': 'Max'},
     'reportSuiteName': 'App',
     'rsid': 'test',
     'siteTitle': 'App',
     'tags': []}


def walk_dict(d, interesting_keys, result, depth=0):
    for k, v in sorted(d.items(), key=lambda x: x[0]):
        if isinstance(v, dict):
            walk_dict(v, interesting_keys, result, depth + 1)
        elif isinstance(v,list):
            for entry in v:
                if isinstance(entry, dict):
                    walk_dict(entry, interesting_keys, result, depth + 1)
        else:
            if k in interesting_keys:
                result[k].append(v)


result = defaultdict(list)
walk_dict(a, ["description", "func", "str", "num"], result)
print(result)
输出

defaultdict(<class 'list'>, {'func': ['container', 'and', 'event', 'event-exists', 'streq', 'attr', 'segment'], 'description': ['e42', 'v44', ''], 'str': ['544']})
defaultdict(,{'func':['container','and','event','event','event exists','streq','attr','segment'],'description':['e42','v44','','','str':['544']})

这些信息是否总是一致的?我的意思是,数据是通过以下方式找到的:
a['definition']['container']['pred']['preds']
。如果它总是在那里,您可以在该列表中进行迭代。还有一个棘手的问题,就是存在嵌套/多个
“func”
键,也就是说,您希望提取每个键的所有值
“func”
,您将无法获得您要查找的结果。不幸的是,数据不一致,可能是由['definition']['container']['pred']['preds']找到的,也可能是在['definition']['container']['pred']['evt']找到的