![](/img/trans.png)
[英]How to extract multiple values of a list of python dictionary (i.e. extract all the values for one key)?
[英]How can I extract all instances of a key from a list within a python dictionary?
我有一個名為“ available_volumes”的python字典。 該詞典包含一個鍵“ Volumes”,並且在該鍵內包含“ VolumeIds”列表。 如何打印此列表中的每個“ VolumeId”?
這是我的“ available_volumes”數據的一些簡化示例數據:
{'Volumes':
[
{'VolumeId': 'vol-0fdfe004dgte6e8385d'},
{'VolumeId': 'vol-0fdfe344d35te6e858f'},
{'VolumeId': 'vol-0fwde024d4gte6e834g'}
]
}
如果我使用“ available_volumes”字典並對其運行以下命令:
myIds = available_volumes ['Volumes'][0]['VolumeId']
我可以獲得第一個位於索引0的VolumeId。但是,如何迭代和打印所有VolumeId?
謝謝。
只需遍歷列表:
for vol in available_volumes['Volumes']:
print(vol['VolumeId'])
使用['Volumes']
查找VolumeId詞典列表。 然后使用for循環遍歷字典列表。 然后使用d['VolumeId']
從內部字典中提取實際值:
>>> available_volumes = {'Volumes':
[
{'VolumeId': 'vol-0fdfe004dgte6e8385d'},
{'VolumeId': 'vol-0fdfe344d35te6e858f'},
{'VolumeId': 'vol-0fwde024d4gte6e834g'}
]
}
>>> for d in available_volumes['Volumes']:
print(d['VolumeId'])
vol-0fdfe004dgte6e8385d
vol-0fdfe344d35te6e858f
vol-0fwde024d4gte6e834g
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.