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

2024-10-04 01:23:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本这样的字典:

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'])

Tags: nameineventnodeforcontainerfunctiondescription
1条回答
网友
1楼 · 发布于 2024-10-04 01:23:51

下面的代码将“有趣”键的值收集到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']})

相关问题 更多 >