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:254500tMESH:D009101tBERN:106985601', 'start': 96}, 0.9999999403953552], [{'end': 268, 'id': 'OMIM:254450tMESH:D055728tBERN:106922101', 'start': 266}, 0.9999996423721313], [{'end': 353, 'id': 'OMIM:254450tMESH:D055728tBERN: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])
Advertisement
Answer
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]]