我有下面的字典(从报告中生成,所以结构可以改变)。我需要深入到字典的深度,找到id
,在本例中是'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75'
,(可以不止1),删除该id,然后移动到上一级,重复相同的操作,直到我到达最后删除的顶部id
。由于存在依赖关系,我需要首先删除孤立的id
,然后移动到顶部。
任何帮助都是值得欣赏的。如果需要任何其他文件/信息,请让我知道。
{
'id': u'4c31d813-a989-47dd-b01b-9a27b8db2dfc',
'snapshots':
[
{
'id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'volumes':
[
{
'id': u'5488de90-50dc-4d72-a6aa-c995422fa179',
'snapshots': [],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'
},
{
'id': u'e566645f-4fb3-4778-be67-447a5bdd678d',
'snapshots':
[
{
'id': u'd637f6ea-4a41-448c-874f-ffe624ddc597',
'volumes':
[
{
'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75',
'snapshots': [],
'snapshot_id': u'd637f6ea-4a41-448c-874f-ffe624ddc597'
}
]
}
],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'},
{
'id': u'196483ee-4f21-4d83-8e15-8caea532b2ab',
'snapshots': [],
'snapshot_id': u'3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5'
}
]
}
],
'snapshot_id': None
}
Python代码
oh=openstack_helper.OpenstackHelper()
def get_objects(item):
items=None
if item == 'stacks':
items=oh.get_stacks()
if item == 'volumes':
items=oh.get_volumes()
if item == 'snapshots':
items=oh.get_snapshots()
return items
def dep_graph(volumes,snapshots,snapshot_id=None):
vol_list=[]
for volume in volumes:
if volume.snapshot_id == snapshot_id:
info={'id':volume.id,'snapshot_id':volume.snapshot_id,'snapshots':[],
}
vol_list.append(info)
for snapshot in snapshots:
for volume in vol_list:
snap_list=[]
if snapshot.volume_id == volume['id']:
info={'id':snapshot.id, 'volumes':[]}
info['volumes'].extend(dep_graph(volumes,snapshots,snapshot.id))
volume['snapshots'].append(info)
return vol_list
if __name__ == '__main__':
volumes = get_objects('volumes')
snapshots = get_objects('snapshots')
output = dep_graph(volumes, snapshots)
print output
https://stackoverflow.com/questions/50642922
复制相似问题