代码之家  ›  专栏  ›  技术社区  ›  bc291

通过python字典递归查找以创建Graphwiz

  •  0
  • bc291  · 技术社区  · 6 年前

    [
        {
            "children": [
                {
                    "children": [
                        {
                            "children": [
                                {
                                    "label": "Something20",
                                    "id": 11
                                },
                                {
                                    "label": "Something19",
                                    "id": 12
                                }
                            ],
                            "label": "Something18",
                            "id": 5
                        },
                        {
                            "children": [
                                {
                                    "label": "Something15",
                                    "id": 13
                                }
                            ],
                            "label": "Something14",
                            "id": 6
                        }
                    ],
                    "label": "Something2",
                    "id": 2
                },
                {
                    "children": [
                        {
                            "children": [
                                {
                                    "label": "Something10",
                                    "id": 14
                                }
                            ],
                            "label": "Something9",
                            "id": 7
                        },
                        {
                            "label": "Something8",
                            "id": 8
                        }
                    ],
                    "label": "Somethin7",
                    "id": 3
                },
                {
                    "children": [
                        {
                            "label": "Something5",
                            "id": 9
                        },
                        {
                            "label": "Something4",
                            "id": 10
                        }
                    ],
                    "label": "Something2",
                    "id": 4
                }
            ],
            "label": "Something1",
            "id": 1
        }
    ]
    

    我如何递归地通过这个dict来生成这样的Graphviz数据:

    Something1->Something2
    Something1->Something7
    

    所以,例如 edges = [] 并附加一些数据作为touples(仅标签):

    [("Something1", "Something2"), ("Something1", "Something7")]
    

    1 回复  |  直到 5 年前
        1
  •  2
  •   Leon    6 年前

    如果要递归地执行此操作,可以有一个函数,它从传递给每个子节点的节点生成一条边,并为每个子节点调用自己。然后,只需为根列表中的每个节点调用它(并且需要收集列表中生成的所有边)。具体实施如下( root

    def get_edges(node):
        if "children" not in node: # ensure the node has children
            return []
        label = node["label"]
        children = node["children"]
        result = [(label, c["label"]) for c in children] # create the edges
        for c in children:
            result.extend(get_edges(c)) # travers the tree recursively and collect all edges
        return result
    
    edges = sum(map(get_edges, root), []) # create and combine all the edge lists
    

    使用您提供的数据运行此代码会产生以下结果:

    [('Something1', 'Something2'), ('Something1', 'Somethin7'), 
     ('Something1', 'Something2'), ('Something2', 'Something18'), 
     ('Something2', 'Something14'), ('Something18', 'Something20'), 
     ('Something18', 'Something19'), ('Something14', 'Something15'), 
     ('Somethin7', 'Something9'), ('Somethin7', 'Something8'), 
     ('Something9', 'Something10'), ('Something2', 'Something5'), 
     ('Something2', 'Something4')]