我有一个像这样的python字典,它表示父子关系。
{'children': [{'child': {'name': 'chi1', 'parents': 'par', 'foo1': 'bar1'}}, {'child': {'name': 'chi2', 'parents': 'par', 'foo7': 'bar7'}}, {'child': {'name': 'chi1_chi1', 'parents': 'chi1', 'foo2': 'bar2', 'children': [{'child': {'name': 'chi1_chi1_chil1', 'foo3': 'bar3'}}, {'child': {'name': 'chi1_chi1_chi2', 'foo4': 'bar4'}}]}}, {'child': {'name': 'par', 'foo5': 'bar5'}}, {'child': {'name': 'chi1_chi2', 'parents': 'chi1, chi2', 'foo6': 'bar6'}}]}
输入字典(以YAML格式显示):
children:
- child:
name: chi1
parents: par
foo1: bar1
- child:
name: chi2
parents: par
foo7: bar7
- child:
name: chi1_chi1
parents: chi1
foo2: bar2
children:
- child:
name: chi1_chi1_chil1
foo3: bar3
- child:
name: chi1_chi1_chi2
foo4: bar4
- child:
name: par
foo5: bar5
- child:
name: chi1_chi2
parents: chi1, chi2 # yay, I have two parents
foo6: bar6
如何将其转换为显示适当的父子关系的树状字典,以便在给定父级名称的情况下获得父级的所有子级
{'children': [{'child': {'name': 'par', 'foo5': 'bar5', 'children': [{'child': {'name': 'chi2', 'foo7': 'bar7', 'parents': 'par', 'children': [{'child': {'name': 'chi1_ch2', 'foo6': 'bar6', 'parents': 'chi1, chi2'}}]}}, {'child': {'name': 'chi1', 'foo1': 'bar1', 'parents': 'par', 'children': [{'child': {'name': 'chi1_chi1', 'foo2': 'bar2', 'parents': 'chi1', 'children': [{'child': {'name': 'chi1_chi1_chi1', 'foo3': 'bar3', 'parents': 'chi1_chi1'}}, {'child': {'name': 'chi1_chi1_chi2', 'foo4': 'bar4', 'parents': 'chi1_chi1'}}]}}, {'child': {'name': 'chi1_chi2', 'parents': 'chi1', 'foo6': 'bar6'}}]}}]}}]}
.
预期结果字典(以YAML格式显示):
children:
- child:
name: par
foo5: bar5
children:
- child:
name: chi2
foo7: bar7
parents: par
children:
- child:
name: chi1_ch2
foo6: bar6
parents: chi1, chi2
- child:
name: chi1
foo1: bar1
parents: par
children:
- child:
name: chi1_chi1
foo2: bar2
parents: chi1
children:
- child:
name: chi1_chi1_chi1
foo3: bar3
parents: chi1_chi1
- child:
name: chi1_chi1_chi2
foo4: bar4
parents: chi1_chi1
- child:
name: chi1_chi2
parents: chi1
foo6: bar6
我一直在努力创造这个几天没有效果。如果有这么多关卡,我不确定如何用编程方式做到这一点。请帮助。提前感谢!
你可以使用递归:
import yaml
data = {'children': [{'child': {'name': 'chi1', 'parents': 'par', 'foo1': 'bar1'}}, {'child': {'name': 'chi2', 'parents': 'par', 'foo7': 'bar7'}}, {'child': {'name': 'chi1_chi1', 'parents': 'chi1', 'foo2': 'bar2', 'children': [{'child': {'name': 'chi1_chi1_chil1', 'foo3': 'bar3'}}, {'child': {'name': 'chi1_chi1_chi2', 'foo4': 'bar4'}}]}}, {'child': {'name': 'par', 'foo5': 'bar5'}}, {'child': {'name': 'chi1_chi2', 'parents': 'chi1, chi2', 'foo6': 'bar6'}}]}
def check_children(node):
if not node['children']:
del node['children']
return node
def build_tree(nodes, parent = ''):
return {'children':[{'child':check_children({**i, 'children':i.get('children', [])+build_tree(nodes, i['name'])['children']})}
for i in nodes if parent in i.get('parents', '').split(', ')]}
result = build_tree([i['child'] for i in data['children']])
print(yaml.dump(result))
输出children:
- child:
children:
- child:
children:
- child:
children:
- child:
foo3: bar3
name: chi1_chi1_chil1
- child:
foo4: bar4
name: chi1_chi1_chi2
foo2: bar2
name: chi1_chi1
parents: chi1
- child:
foo6: bar6
name: chi1_chi2
parents: chi1, chi2
foo1: bar1
name: chi1
parents: par
- child:
children:
- child:
foo6: bar6
name: chi1_chi2
parents: chi1, chi2
foo7: bar7
name: chi2
parents: par
foo5: bar5
name: par