【问题标题】:python create hierarchy from parent-child listpython从父子列表创建层次结构
【发布时间】:2022-01-27 18:56:23
【问题描述】:

给定以下列表:

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'}
]

我想创建列表的分层字典 到目前为止,我制作的这段代码运行良好,但由于某种原因,孩子 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

编辑: 上面的代码产生了这个输出:

{'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': []}}]}}

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

【问题讨论】:

  • 您能否提供所需输出的示例?
  • 如果你运行代码你可以看到问题。但我已经编辑了我的问题

标签: python recursion


【解决方案1】:

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:

因为c3a 中出现了两次,所以您将其子代添加两次。

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

data[k] = {'child': k, 'child_name': name, 'children': []}
data[k]['children'].append(build(k))

【讨论】:

  • 谢谢!这就是问题所在!
猜你喜欢
  • 2018-01-21
  • 2016-02-27
  • 1970-01-01
  • 2010-09-24
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2021-04-26
  • 2019-02-13
相关资源
最近更新 更多