Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List of dicts to multilevel dict based on depth info

I have some data, more or less like this:

[
{"tag": "A", "level":0},
{"tag": "B", "level":1},
{"tag": "D", "level":2},
{"tag": "F", "level":3},
{"tag": "G", "level":4},
{"tag": "E", "level":2},
{"tag": "H", "level":3},
{"tag": "I", "level":3},
{"tag": "C", "level":1},
{"tag": "J", "level":2},
]

I want to turn it into a multilevel dict based on depth level (key "level"):

{
    "A": {"level": 0, "children": {
            "B": {"level": 1, "children": {
                    "D": {"level": 2, "children": {
                            "F": {"level": 3, "children": {
                                    "G": {"level": 4, "children": {}}}}}},
                    "E": {"level": 2, "children": {
                            "H": {"level": 3, "children": {}},
                            "I": {"level": 3, "children": {}}}}}},
            "C": {"level": 1, "children": {
                    "J": {"level": 2, "children": {}}}}}}
}

All I can come up with right now is this little piece of code... which obviously breaks after few items:

def list2multilevel(list):
    children = {}
    parent = list.pop(0)
    tag = parent.get("Tag")
    level = parent.get("Level")
    for child in list:
        ctag = child.get("Tag")
        clevel = child.get("Level")
        if clevel == level + 1:
            children.update(list2multilevel(list))
        elif clevel <= level:
            print(clevel, level)
            break
    return {tag: children}

Originally sat down to it on Friday and it was supposed to be just a small exercise....

like image 660
MrPilipo Avatar asked Apr 06 '20 09:04

MrPilipo


2 Answers

data = [
    {"tag": "A", "level": 0},
    {"tag": "B", "level": 1},
    {"tag": "D", "level": 2},
    {"tag": "F", "level": 3},
    {"tag": "G", "level": 4},
    {"tag": "E", "level": 2},
    {"tag": "H", "level": 3},
    {"tag": "I", "level": 3},
    {"tag": "C", "level": 1},
    {"tag": "J", "level": 2},
]

root = {'level': -1, 'children': {}}
parents = {-1: root}
for datum in data:
    level = datum['level']
    parents[level] = parents[level - 1]['children'][datum['tag']] = {
        'level': datum['level'],
        'children': {},
    }
result = root['children']
print(result)

output:

{'A': {'level': 0, 'children': {'B': {'level': 1, 'children': {'D': {'level': 2, 'children': {'F': {'level': 3, 'children': {'G': {'level': 4, 'children': {}}}}}}, 'E': {'level': 2, 'children': {'H': {'level': 3, 'children': {}}, 'I': {'level': 3, 'children': {}}}}}}, 'C': {'level': 1, 'children': {'J': {'level': 2, 'children': {}}}}}}}

restriction:

  • level >= 0
  • Any level cannot be bigger than +1 of max level appeared before.

explanation:

  • parents is a dictionary to remember last element for each level.
  • root is a starting point(dummy element).
  • logic:
    • Start with -1 level which indicates the root.
    • Make an item and register it into parent's children.
    • Update same item to parents dictionary.
    • Repeat.
    • Extract root['children'].
like image 53
Boseong Choi Avatar answered Oct 24 '22 14:10

Boseong Choi


Other solution using recursion (same restrictions as with Boseong Choi's answer):

data = [
    {"tag": "A", "level": 0},
    {"tag": "B", "level": 1},
    {"tag": "D", "level": 2},
    {"tag": "F", "level": 3},
    {"tag": "G", "level": 4},
    {"tag": "E", "level": 2},
    {"tag": "H", "level": 3},
    {"tag": "I", "level": 3},
    {"tag": "C", "level": 1},
    {"tag": "J", "level": 2},
]

def make_node(dic):
    node = dic.copy()
    node["children"] = {}
    tag = node.pop("tag")
    return tag, node

def add_child(parent, child, tag):
    assert child["level"] > parent["level"]
    if child["level"] == parent["level"] + 1:
        parent["children"][tag] = child
        return True
    for node in parent["children"].values():
        if add_child(node, child, tag):
            return True
    return False

def parse(lst):
    assert lst[0]["level"] == 0
    root_tag, root = make_node(lst[0])
    for item in lst[1:]:
        tag, node = make_node(item)
        add_child(root, node, tag)


print(parse(data))
like image 28
bruno desthuilliers Avatar answered Oct 24 '22 14:10

bruno desthuilliers