从深度遍历嵌套字典并移至顶部 [英] Traverse nested dictionary from depth and move to top
问题描述
我有下面的词典(由报告生成,因此结构可以更改).
我需要转到字典的深度,找到id
(在这种情况下为'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75'
)(可以大于1),删除该ID,然后向上移动一级并重复相同的操作,直到我到达顶部id
,我终于删除了它.由于存在依赖关系,因此我需要先删除孤立的id
,然后再移至顶部.
I have below dictionary (generated from a report, so the structure can change).
I need to go to the depth of the dictionary, find the id
which in this case is 'id': u'ef3c8cf1-0987-4e56-a6d5-763c42be1f75'
, (there can be more than 1), delete that id and then move to one level up and repeat the same till I get to the top id
which I finally delete. Since there is a dependency, I need to delete the orphan id
first and then move to the top.
任何帮助都是有意义的.如果需要任何其他文件/信息,请告诉我.
Any help is appreciable. If any other file/information is needed, please let me know.
{
'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
推荐答案
这是我建议的解决方案:
Here is the solution I propose:
def remove_id(data):
if isinstance(data, List):
return [remove_id(sub_data) for sub_data in data]
if isinstance(data, Dict):
return {key: remove_id(value) for key, value in data.items()
if key != 'id'}
return data
结果:
{'snapshot_id': None,
'snapshots': [{'volumes': [{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': []},
{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': [{'volumes': [{'snapshot_id': 'd637f6ea-4a41-448c-874f-ffe624ddc597',
'snapshots': []}]}]},
{'snapshot_id': '3ddc7ddd-02ca-4669-a0cb-fb0d56a4a6f5',
'snapshots': []}]}]}
这篇关于从深度遍历嵌套字典并移至顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!