Daftar dicts ke dict multilevel berdasarkan info kedalaman

9

Saya punya beberapa data, kurang lebih seperti ini:

[
{"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},
]

Saya ingin mengubahnya menjadi dict multilevel berdasarkan level kedalaman (kunci "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": {}}}}}}
}

Yang bisa saya pikirkan sekarang adalah sepotong kecil kode ini ... yang jelas-jelas rusak setelah beberapa item:

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}

Awalnya duduk di sana pada hari Jumat dan seharusnya hanya latihan kecil ....

Pilipo
sumber

Jawaban:

6
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)

keluaran:

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

larangan:

  • level >= 0
  • Apa levelpun tidak boleh lebih besar +1dari level maks yang muncul sebelumnya.

penjelasan:

  • parents adalah kamus untuk mengingat elemen terakhir untuk setiap level.
  • root adalah titik awal (elemen dummy).
  • logika:
    • Mulai dengan -1level yang mengindikasikan root.
    • Buat barang dan daftarkan ke barang orang tua children.
    • Perbarui item yang sama ke parentskamus.
    • Ulang.
    • Ambil root['children'].
Boseong Choi
sumber
1

Solusi lain menggunakan rekursi (pembatasan yang sama dengan jawaban Boseong Choi):

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))
perusak Bruno
sumber
-1

Anda dapat menggunakan rekursi:

from itertools import groupby as gb
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 to_tree(d, s = 0):
  v = [list(b) for _, b in gb(d, key=lambda x:x['level'] == s)]
  if len(v) == 1:
     return {i['tag']:{'level':s, 'children':{}} for i in v[0]}
  return {v[i][0]['tag']:{'level':s, 'children':to_tree(v[i+1], s+1)} for i in range(0, len(v), 2)}

import json
print(json.dumps(to_tree(data), indent=4))

Keluaran:

{
  "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": {}
                }
            }
         }
      }
   }
}
Ajax1234
sumber
Mengapa downvotes?
Ajax1234