简体   繁体   中英

Accessing nested dictionary values in python

I am new to Python dictionaries, i'm trying to extract the numbers as positions inside the dictionary value denotations, sub category 'span'. The dictionary looks like this:

z = {'denotations': [{'id': ['OMIM:254500', 'MESH:D009101', 'BERN:106985601'],
                  'obj': 'disease',
                  'span': {'begin': 96, 'end': 112}},
                 {'id': ['OMIM:254450', 'MESH:D055728', 'BERN:106922101'],
                  'obj': 'disease',
                  'span': {'begin': 266, 'end': 268}},
                 {'id': ['OMIM:254450', 'MESH:D055728', 'BERN:106922101'],
                  'obj': 'disease',
                  'span': {'begin': 351, 'end': 353}}],
 'logits': {'disease': [[{'end': 112,
                          'id': 'OMIM:254500\tMESH:D009101\tBERN:106985601',
                          'start': 96},
                         0.9999999403953552],
                        [{'end': 268,
                          'id': 'OMIM:254450\tMESH:D055728\tBERN:106922101',
                          'start': 266},
                         0.9999996423721313],
                        [{'end': 353,
                          'id': 'OMIM:254450\tMESH:D055728\tBERN:106922101',
                          'start': 351},
                         0.9999995231628418]]}

I'm only interested in the denotations category, more so the numbers held inside span . I can only manage to extract the denotation information print(z["denotations"]) and I'm a bit stuck on how to go further into the dictionary, example:

Is it possible to extract the span information:

print(z['span'])

Output:
'span': {'begin': 96, 'end': 112}},
'span': {'begin': 266, 'end': 268}},
'span': {'begin': 351, 'end': 353}}]

or even store just the numbers as positions?

positions = ([96,112],[266, 268], [351, 353])

The trick is to recognise that z['denotations'] is a list not a dictionary. Therefore, you need to iterate over this list to access each dictionary containing the spans.

positions = []
for item in z['denotations']:
    positions.append([item['span']['begin'], item['span']['end']])
print(positions)

Output

[[96, 112], [266, 268], [351, 353]]

I think what you're looking for is a list comprehension?

return [x['span'] for x in z['denotations']]

or for the positions, use:

return [[x['span']['begin'], x['span']['end']] for x in z['denotations']]

You can use list comprehension:

>>> [(el["span"]["begin"], el["span"]["end"]) for el in z["denotations"]]
[(96, 112), (266, 268), (351, 353)]

z["denotations"] is a list of objects, and for every object you need to extract begin and end from the span dictionary.

You can also do in this way

from functools import reduce
myList=z['denotations']

# use python reduce 

def inside_get(dictionary, *keys):
   return reduce(lambda d, key: d.get(key) if d else None, keys, dictionary)

for item in lt:
  result= inside_get(item, 'span')
  print(result)

Output:
{'begin': 351, 'end': 353}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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