Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-从元组列表生成字典(树)_Python_Tree - Fatal编程技术网

Python-从元组列表生成字典(树)

Python-从元组列表生成字典(树),python,tree,Python,Tree,我有以下清单:- a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)] 这是一个元组列表。元组中的元素的格式为(Id,ParentId)只要Id==ParentId它的根节点。列表可以是元组的任意顺序 我想使用上面的元组列表生成以下字典 output = [{ 'id': 1, 'children': [{ { 'id': 3,

我有以下清单:-

a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)]
这是一个元组列表。元组中的元素的格式为
(Id,ParentId)
只要
Id==ParentId
它的根节点。列表可以是元组的任意顺序

我想使用上面的元组列表生成以下字典

output = [{
    'id': 1,
    'children': [{
        {
            'id': 3,
            'children': [{
                {
                    'id': 5
                },
                {
                    'id': 4
                },
                {
                    'id': 6
                }
            }]
        },
        {
            'id': 2
        }
    }]
}, {
    'id': 7,
    'children': [{
        {
            'id': 9
        },
        {
            'id': 8
        }
    }]
}]
ie(以图形的形式-a forrest)

我的最终输出应该是上面给出的字典

我尝试了以下方法:-

# set the value of nested dictionary.
def set_nested(d, path, value):
    reduce(lambda d, k: d.setdefault(k, {}), path[:-1], d)[path[-1]] = value
    return d

# returns the path of any node in list format
def return_parent(list, child):
    for tuple in list:
        id, parent_id = tuple
        if parent_id == id == child:
            return [parent_id]
        elif id == child:
            return [child] + return_parent(list, parent_id)

paths = []
temp = {}
for i in a:
    id, parent_id = i
    temp[id] = {'id': id}
    path = return_parent(a, id)[::-1]
    paths.append(path) # List of path is created

d = {}
for path in paths:
    for n, id in enumerate(path):
        set_nested(d, path[:n + 1], temp[id]) # setting the value of nested dictionary.

print d
我尝试的解决方案如下:-

# set the value of nested dictionary.
def set_nested(d, path, value):
    reduce(lambda d, k: d.setdefault(k, {}), path[:-1], d)[path[-1]] = value
    return d

# returns the path of any node in list format
def return_parent(list, child):
    for tuple in list:
        id, parent_id = tuple
        if parent_id == id == child:
            return [parent_id]
        elif id == child:
            return [child] + return_parent(list, parent_id)

paths = []
temp = {}
for i in a:
    id, parent_id = i
    temp[id] = {'id': id}
    path = return_parent(a, id)[::-1]
    paths.append(path) # List of path is created

d = {}
for path in paths:
    for n, id in enumerate(path):
        set_nested(d, path[:n + 1], temp[id]) # setting the value of nested dictionary.

print d
我得到的结果是

{
    '1': {
        '3': {
            '6': {
                'id': '6'
            },
            '5': {
                'id': '5'
            },
            'id': '3',
            '4': {
                '10': {
                    'id': '10'
                },
                'id': '4'
            }
        },
        '2': {
            'id': '2'
        },
        'id': '1'
    },
    '7': {
        '9': {
            'id': '9'
        },
        '8': {
            'id': '8'
        },
        'id': '7'
    }
}

我接近它,但无法得到确切的输出。还有,有没有更好的解决办法

这里有一个更简单的方法。(正如我从Thomas answer中了解到的,节点可以按任何顺序进行编辑):过程1创建节点(即,将它们添加到节点字典中),而过程2则创建parentchildren结构

做出以下假设:无循环(Garret R指出,在这种情况下,不清楚预期输出是什么),无缺失边,无缺失树根

a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)]

# pass 1: create nodes dictionary
nodes = {}
for i in a:
    id, parent_id = i
    nodes[id] = { 'id': id }

# pass 2: create trees and parent-child relations
forest = []
for i in a:
    id, parent_id = i
    node = nodes[id]

    # either make the node a new tree or link it to its parent
    if id == parent_id:
        # start a new tree in the forest
        forest.append(node)
    else:
        # add new_node as child to parent
        parent = nodes[parent_id]
        if not 'children' in parent:
            # ensure parent has a 'children' field
            parent['children'] = []
        children = parent['children']
        children.append(node)

print forest
编辑:为什么您的解决方案不能按预期工作


这里有一个关于顶层的提示:您想要获得的输出是一个树列表。但是,您正在处理的变量(d)需要是一个字典,因为在function set_nested中,您将setdefaults方法应用于该变量。

根据需要,此解决方案不考虑节点的顺序

a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)]
b = [(8, 7), (5, 3), (2, 1), (1, 1), (3, 1), (7, 7), (4, 3), (6, 3), (9, 7)]

def build_forest( nodelist ):
    forest = [] 
    nodes = {}  

    id = 'id'
    children = 'children'

    for node_id, parent_id in nodelist:
        #create current node if necessary
        if not node_id in nodes:
            node = { id : node_id }
            nodes[node_id] = node
        else:
            node = nodes[node_id]

        if node_id == parent_id:
            #add node to forrest
            forest.append( node )
        else:
            #create parent node if necessary
            if not parent_id in nodes:
                parent = { id : parent_id }
                nodes[parent_id] = parent
            else:
                parent = nodes[parent_id]
            #create children if necessary
            if not children in parent:
                parent[children] = []
            #add node to children of parent
            parent[children].append( node )

    return forest    

print( build_forest( a ) )
print( build_forest( b ) )

为了简化此过程,让我们定义一个简单的关系对象:

class Node(dict):

    def __init__(self, uid):
        self._parent = None  # pointer to parent Node
        self['id'] = uid  # keep reference to id #            
        self['children'] = [] # collection of pointers to child Nodes

    @property
    def parent(self):
        return self._parent  # simply return the object at the _parent pointer

    @parent.setter
    def parent(self, node):
        self._parent = node
        # add this node to parent's list of children
        node['children'].append(self)  
接下来定义如何将节点集合彼此关联。我们将使用dict保存指向每个单独节点的指针:

def build(idPairs):
    lookup = {}
    for uid, pUID in idPairs:
        # check if was node already added, else add now:
        this = lookup.get(uid)
        if this is None:
            this = Node(uid)  # create new Node
            lookup[uid] = this  # add this to the lookup, using id as key

        if uid != pUID:
            # set this.parent pointer to where the parent is
            parent = lookup[pUID]
            if not parent:
                # create parent, if missing
                parent = Node(pUID)  
                lookup[pUID] = parent
            this.parent = parent

    return lookup
现在,获取输入数据并将其关联起来:

a = [(1, 1), (2, 1), (3, 1), (4, 3), (5, 3), (6, 3), (7, 7), (8, 7), (9, 7)]
lookup = build(a)  # can look at any node from here.

for uid in [1, 3, 4]:
    parent = lookup[uid].parent
    if parent:
        parent = parent['id']
    print "%s's parent is: %s" % (uid, parent)
最后,获取输出:您很可能希望将数据根化为一个唯一树列表,而不是一个字典,但是您可以选择自己喜欢的

roots = [x for x in lookup.values() if x.parent is None]

# and for nice visualization:
import json
print json.dumps(roots, indent=4)
屈服:

[
    {
        "id": 1, 
        "children": [
            {
                "id": 2, 
                "children": []
            }, 
            {
                "id": 3, 
                "children": [
                    {
                        "id": 4, 
                        "children": []
                    }, 
                    {
                        "id": 5, 
                        "children": []
                    }, 
                    {
                        "id": 6, 
                        "children": []
                    }
                ]
            }
        ]
    }, 
    {
        "id": 7, 
        "children": [
            {
                "id": 8, 
                "children": []
            }, 
            {
                "id": 9, 
                "children": []
            }
        ]
    } ]

只是好奇,但你保证有有效的输入吗?没有循环(例如,1是2的父对象,而2是1的父对象)?