简体   繁体   English

如何从有序邻接表构建递归字典树

[英]How to build a recursive dictionary tree from an ordered adjacency list

I've been trying to figure this out all day and Im at my wits end. 我整天都在努力解决这个问题,而林恩则无能为力。 Maybe I'm just getting to old for this. 也许我为此变老了。

I'm trying to build a tree for the load_bulk feature on django-treebeard as specified here 我试图建立一个用于Django的树胡的load_bulk功能树作为指定在这里

To save you looking, it should look like this: 为了让您看起来更轻松,它应如下所示:

data = [{'data':{'desc':'1'}},
         {'data':{'desc':'2'}, 'children':[
          {'data':{'desc':'21'}},
          {'data':{'desc':'22'}},
          {'data':{'desc':'23'}, 'children':[
            {'data':{'desc':'231'}},
          ]},
          {'data':{'desc':'24'}},
        ]},
        {'data':{'desc':'3'}},
        {'data':{'desc':'4'}, 'children':[
          {'data':{'desc':'41'}},
        ]},
]

'data' holds the record, and if it has children, 'children' is a list of more 'data' dicts (that can also contain a list of children and so on recursively) “数据”保留记录,并且如果有子记录,“子项”是更多“数据”字典的列表(也可以包含子项列表,以此类推)

I get the data as an ordered list (ordered as in depth first, not by id): 我将数据作为有序列表(先按深度排序,而不按ID排序):

eg: 例如:

[
    {'id': 232, 'name': 'jon', 'parent': 'None'}
    {'id': 3522, 'name': 'dave', 'parent': '232'}
    {'id': 2277, 'name': 'alice', 'parent': '3522'}
    {'id': 119, 'name': 'gary', 'parent': '232'}
    {'id': 888, 'name': 'gunthe', 'parent': '119'}
    {'id': 750, 'name': 'beavis', 'parent': 'None'}
    {'id': 555, 'name': 'urte', 'parent': '750'}
]

How can I transform it into a treebeard compliant dictionary that would look like this (typo's excepted): 我如何将其转换为类似于树须的字典(典型错误除外):

[
{'data': {'id': 232, 'name': 'jon', 'parent': 'None'},
 'children': [
              {'data': {'id': 3522, 'name': 'dave', 'parent': '232'},
               'children': [
                            {'data': {'id': 2277, 'name': 'alice', 'parent': '3522'}}
                           ]
              }
              {'data': {'id': 119, 'name': 'gary', 'parent': '232'},
               'children': [
                            {'id': 888, 'name': 'gunthe', 'parent': '119'}
                           ]
              }
             ]
{'data': {'id': 750, 'name': 'beavis', 'parent': 'None'},
 'children': [
              {'id': 555, 'name': 'urte', 'parent': '750'}
             ]
}

] ]

I guess I need some kind of recursion function seeing as its a recursive structure but all my attempts have failed. 我想我需要某种递归函数,将其视为递归结构,但是我的所有尝试都失败了。 My brain doesnt do recursion so good. 我的大脑不做递归那么好。

I did a lot of searching and found mostly solutions pertaining to lists or other structures that i cant mould to fit. 我进行了很多搜索,发现大部分与列表或其他我无法适应的结构有关的解决方案。 I'm a relative noob. 我是一个相对的菜鸟。 ps i had more fun manually typing out the example than i did the rest of day (apart from dinner time). PS:我手动键入示例比在一天的其余时间(除了晚餐时间)更有趣。

Maybe there are better ways, but here is one solution: 也许有更好的方法,但这是一种解决方案:

users = [
    {
        'id': 232,
        'name': 'jon',
        'parent': None
    },
    {
        'id': 3522,
        'name': 'dave',
        'parent': 232
    },
    {
        'id': 2277,
        'name': 'alice',
        'parent': 3522
    },
    {
        'id': 119,
        'name': 'gary',
        'parent': 232
    },
    {
        'id': 888,
        'name': 'gunthe',
        'parent': 119
    },
    {
        'id': 750,
        'name': 'beavis',
        'parent': None
    },
    {
        'id': 555,
        'name': 'urte',
        'parent': 750
    }
]

users_map = {}
for user in users:
    users_map[user['id']] = user

users_tree = []
for user in users:
    if user['parent'] is None:
        users_tree.append(user)
    else:
        parent = users_map[user['parent']]
        if 'childs' not in parent:
            parent['childs'] = []
        parent['childs'].append(user)

print(users_tree)

#user as {data: user, childs: []}

users_map = {}
for user in users:
    users_map[user['id']] = {'data': user, 'childs': []}

users_tree = []
for user in users:
    if user['parent'] is None:
        users_tree.append(users_map[user['id']])
    else:
        parent = users_map[user['parent']]
        parent['childs'].append(users_map[user['id']])

print(users_tree)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM