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:
JavaScript
x
22
22
1
z = {'denotations': [{'id': ['OMIM:254500', 'MESH:D009101', 'BERN:106985601'],
2
'obj': 'disease',
3
'span': {'begin': 96, 'end': 112}},
4
{'id': ['OMIM:254450', 'MESH:D055728', 'BERN:106922101'],
5
'obj': 'disease',
6
'span': {'begin': 266, 'end': 268}},
7
{'id': ['OMIM:254450', 'MESH:D055728', 'BERN:106922101'],
8
'obj': 'disease',
9
'span': {'begin': 351, 'end': 353}}],
10
'logits': {'disease': [[{'end': 112,
11
'id': 'OMIM:254500tMESH:D009101tBERN:106985601',
12
'start': 96},
13
0.9999999403953552],
14
[{'end': 268,
15
'id': 'OMIM:254450tMESH:D055728tBERN:106922101',
16
'start': 266},
17
0.9999996423721313],
18
[{'end': 353,
19
'id': 'OMIM:254450tMESH:D055728tBERN:106922101',
20
'start': 351},
21
0.9999995231628418]]}
22
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:
JavaScript
1
7
1
print(z['span'])
2
3
Output:
4
'span': {'begin': 96, 'end': 112}},
5
'span': {'begin': 266, 'end': 268}},
6
'span': {'begin': 351, 'end': 353}}]
7
or even store just the numbers as positions?
JavaScript
1
2
1
positions = ([96,112],[266, 268], [351, 353])
2
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.
JavaScript
1
5
1
positions = []
2
for item in z['denotations']:
3
positions.append([item['span']['begin'], item['span']['end']])
4
print(positions)
5
Output
JavaScript
1
2
1
[[96, 112], [266, 268], [351, 353]]
2