我有以下字典X:
X ={'Deck': [{'geometry': [{'x': 51, 'y': 55},
{'x': 58, 'y': 47},
{'x': 25, 'y': 19},
{'x': 17, 'y': 19},
{'x': 17, 'y': 27}]}],
'Superstructure': [{'geometry': [{'x': 52, 'y': 55},
{'x': 59, 'y': 48},
{'x': 66, 'y': 54},
{'x': 60, 'y': 62}]}]}
我想执行以下操作,并打印出“ Deck”“ Geometry”的所有“ x”值:
for row in X.get('Deck','geometry'):
print(row.get('x'))
很显然,这是行不通的,我缺少一些基本知识。
答案 0 :(得分:2)
这将为x
/ Deck
打印所有geometry
:
>>> [i['x'] for i in X['Deck'][0]['geometry']]
[51, 58, 25, 17, 17]
您几乎完全正确,但是您需要使用X.get('Deck','geometry')
来代替X['Deck'][0]['geometry']
,因为那里有list
的某个地方=)