python 从父子列表创建层次结构

Gab*_*bor 1 python recursion

给出以下列表:

a = [
{'parent': 'p1', 'parent_name': 'pn1', 'child': 'c1', 'child_name': 'cn1'},
{'parent': 'p1', 'parent_name': 'pn1', 'child': 'c2', 'child_name': 'cn2'},
{'parent': 'c1', 'parent_name': 'cn1', 'child': 'c3', 'child_name': 'cn3'},
{'parent': 'c1', 'parent_name': 'cn1', 'child': 'c4', 'child_name': 'cn4'},
{'parent': 'c4', 'parent_name': 'cn4', 'child': 'c5', 'child_name': 'cn5'},
{'parent': 'c2', 'parent_name': 'cn2', 'child': 'c6', 'child_name': 'cn6'},
{'parent': 'c3', 'parent_name': 'cn3', 'child': 'c7', 'child_name': 'cn7'}
]
Run Code Online (Sandbox Code Playgroud)

我想创建一个列表的分层字典到目前为止,我已经编写了运行良好的代码,但由于某种原因,子 c3 和 c4 重复了两次。我哪里做错了?

def build(key):

    children = [(item['child'], item['child_name']) for item in a if item['parent'] == key]

    data = {}
    for k, name in children:
        data[k] = {'child': k, 'child_name': name, 'children': []}
        for item in a:
            if item['parent'] == k:
                data[k]['children'].append(build(k))

    return data
Run Code Online (Sandbox Code Playgroud)

编辑: 上面的代码产生以下输出:

{'c1': {'child': 'c1',
        'child_name': 'cn1',
        'children': [{'c3': {'child': 'c3',
                             'child_name': 'cn3',
                             'children': [{'c7': {'child': 'c7',
                                                  'child_name': 'cn7',
                                                  'children': []}}]},
                      'c4': {'child': 'c4',
                             'child_name': 'cn4',
                             'children': [{'c5': {'child': 'c5',
                                                  'child_name': 'cn5',
                                                  'children': []}}]}},
                     {'c3': {'child': 'c3',
                             'child_name': 'cn3',
                             'children': [{'c7': {'child': 'c7',
                                                  'child_name': 'cn7',
                                                  'children': []}}]},
                      'c4': {'child': 'c4',
                             'child_name': 'cn4',
                             'children': [{'c5': {'child': 'c5',
                                                  'child_name': 'cn5',
                                                  'children': []}}]}}]},
 'c2': {'child': 'c2',
        'child_name': 'cn2',
        'children': [{'c6': {'child': 'c6',
                             'child_name': 'cn6',
                             'children': []}}]}}
Run Code Online (Sandbox Code Playgroud)

我需要完全相同的输出,但显然没有重复(这里 c1 的子项重复两次)

Del*_*lan 5

c3例如,由于下面第 2 行, 被重复两次:

data[k] = {'child': k, 'child_name': name, 'children': []}
for item in a: # 'c3' will be found twice in a
  if item['parent'] == k:
Run Code Online (Sandbox Code Playgroud)

因为c3在 中出现两次a,所以您将其子项添加两次。

我不知道为什么你需要做这个循环。data[k]['children'].append(build(k))对我来说,如果您删除该循环并执行以下操作,它看起来会起作用data[k] = {'child': k, 'child_name': name, 'children': []}

data[k] = {'child': k, 'child_name': name, 'children': []}
data[k]['children'].append(build(k))
Run Code Online (Sandbox Code Playgroud)