简体   繁体   English

从子字典中获取键,并从其中的键值对列表中获取值

[英]Get the key from a sub-dictionary and a value from a list of key-val pairs within it

I have a dictionary called metadata.我有一本叫做元数据的字典。 It has a length of 2 keys, 'status' and 'data'.它的长度为 2 个键,“状态”和“数据”。 I am only interested in the key 'data' and what lies within it.我只对关键的“数据”以及其中的内容感兴趣。 The subdictionary 'data' has 1,136 keys.子字典“数据”有 1,136 个键。

{'status': 'success',
 'data': {'async_tasks_count': [{'type': 'gauge',
    'help': 'Total number of async tasks spawned using spawn',
    'unit': ''}],
  'async_tasks_time_histogram': [{'type': 'histogram',
    'help': 'Time taken by async tasks',
    'unit': ''}],
  'attestation_production_cache_interaction_seconds': [{'type': 'histogram',
    'help': 'Time spent interacting with the attester cache',
    'unit': ''}],
  'attestation_production_cache_prime_seconds': [{'type': 'histogram',
    'help': 'Time spent loading a new state from the disk due to a cache miss',
    'unit': ''}]}

Within 'data' I need the Key and the Value of the 1st position (ie second elelment).在“数据”中,我需要第一个位置(即第二个元素)的键和值。 For example the first Key within 'data' would be 'async_tasks_count' and the Value I need is 'Total number of async tasks spawned using spawn', the second Value.例如,“数据”中的第一个键是“async_tasks_count”,我需要的值是“使用 spawn 生成的异步任务总数”,第二个值。

It would be ideal to have them returned like:让他们像这样返回是理想的:

('async_tasks_count' : 'Total number of async tasks spawned using spawn', 'async_tasks_time_histogram' : 'Time taken by async tasks', 'attestation_production_cache_interaction_seconds': 'Time spent interacting with the attester cache', 'attestation_production_cache_prime_seconds': 'Time spent loading a new state from the disk due to a cache miss') ('async_tasks_count':'使用 spawn 生成的异步任务总数','async_tasks_time_histogram':'异步任务花费的时间','attestation_production_cache_interaction_seconds':'与证明者缓存交互所花费的时间','attestation_production_cache_prime_seconds':'加载所花费的时间由于缓存未命中而导致磁盘的新状态')

I'm using Python3我正在使用 Python3

You can loop through data dictionary and then extract help property from each item.您可以遍历data字典,然后从每个项目中提取help属性。

The item is the dictionary with the data property.item是具有data属性的字典。

result = {}
for key, value in metadata['data'].items():
    result[ key ] = value[0].get('help')
    
print(result) # for viewing the result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM