在python字典中进行递归查找以创建Graphwiz

时间:2018-08-10 14:27:29

标签: python dictionary recursion

我有这样的身体字典:

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

我如何递归地搜索该命令以生成Graphviz数据,如下所示:

Something1->Something2
Something1->Something7

例如称为edges = []的列表,并将一些数据附加到列表中(仅标签):

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

生成数据后,我可以简单地在GraphWiz(生成PNG图像)中选对象,以表示所提供字典的树结构。

1 个答案:

答案 0 :(得分:2)

如果要递归执行此操作,则可以使用一个函数,该函数从传递给每个子节点的节点生成边,并为每个子节点调用自身。然后,您要做的就是为根列表中的每个节点调用它(并且您需要在列表中收集所有生成的边)。可以按以下方式实现(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')]