Python循环遍历dict列表以查找属性
我有一个复杂的JSON结构,已加载到dict中:Python循环遍历dict列表以查找属性,python,list,dictionary,Python,List,Dictionary,我有一个复杂的JSON结构,已加载到dict中: { "assets": [ { "account": "Prod", "distributiongroups": [], "name": "Admin", "networks": [ { ... }, { "account": "Dev", "distributiongroups": []
{
"assets": [
{
"account": "Prod",
"distributiongroups": [],
"name": "Admin",
"networks": [
{
...
},
{
"account": "Dev",
"distributiongroups": []
...
我想得到一个所有帐户名的列表,我的问题是
accounts = data['assets'][0]['name']
我刚刚得到第一个帐户,有没有简单的方法来循环所有列表项而不编写长循环?差不多
accounts = data['assets'][0:]['name']
是,使用从循环生成新列表:
accounts = [account['name'] for account in data['assets']]
这将获取列表中由data['assets']
引用的每个字典的'name'
值查看dpath可能会很有用。