python列表/序列格式

时间:2012-04-06 06:28:43

标签: python list sequence

我有一个列表,列表如下所示。

[[1,3],[2,4],[3,1],[4,0],[5,1],[6,0],[7,1],[8,0 ],[9,1],[10,0],[11,3],[12,1],[13,0],[14,1],[15,0],[16,1], [17,0],[18,4],[19,1],[20,0],[21.1],[22,0],[23,1],[24,2],[25 ,0],[26,0],[27,1],[28,0] .........]

或以图形方式输入我的输入列表:

[1,3]
    [2,4]
        [3,1]
            [4,0]
        [5,1]
            [6,0]
        [7,1]
            [8,0]
        [9,1]
            [10,0]
    [11,3]
        [12,1]
            [13,0]
        [14,1]
            [15,0]
        [16,1]
            [17,0]
    [18,4]
        [19,1]
            [20,0]
        [21,1]
            [22,0]
        [23,1]
            [24,2]
                [25,0]
                [26,0]
        [27,1]
            [28,0]

在上面的输入中,列表的第一个值(零位)是项目序列(从上到下可以看到它们),第二个值是它拥有的孩子的数量!

我希望我的输出在第三个值(第二个位置)我想要它的父母,如下例所示..。

我希望获得如下输出: [[1,3,0],[2,4,1],[3,1,2],[4,0,3],[5,1,2],[6,0,5],[ 7,1,2],[8,0,7],[9,1,2],[10,0,9],[11,3,1],[12,1,11],[13, 0,12],[14,1,11],[15,0,14],[16,1,11],[17,0,16],[18,4,1],[19,1, 18],[20,0,19],[21,1,18],[22,0,21],[23,1,18],[24,2,23],[25,0,24] [26,0,24],[27,1,18],[28,0,27] .....]

以图形方式输出所需的输出:

[1,3,0]
    [2,4,1]
        [3,1,2]
            [4,0,3]
        [5,1,2]
            [6,0,5]
        [7,1,2]
            [8,0,7]
        [9,1,2]
            [10,0,9]
    [11,3,1]
        [12,1,11]
            [13,0,12]
        [14,1,11]
            [15,0,14]
        [16,1,11]
            [17,0,16]
    [18,4,1]
        [19,1,18]
            [20,0,19]
        [21,1,18]
            [22,0,21]
        [23,1,18]
            [24,2,23]
                [25,0,24]
                [26,0,24]
        [27,1,18]
            [28,0,27]

任何想法如何解决?

3 个答案:

答案 0 :(得分:3)

你可以用这样的迭代器来实现:

def add_parent_info(it, parent=0):
    me, num_children = it.next() # For Python 3.x use next(it)
    yield [me, num_children, parent]
    for i in range(num_children):
        for item in add_parent_info(it, me):
            yield item

用法:

>>> a = [[1,3],[2,4],[3,1],[4,0],[5,1],[6,0],[7,1],[8,0],[9,1],[10,0],[11,3],[12,1],[13,0],[14,1],[15,0],[16,1],[17,0],[18,4],[19,1],[20,0],[21,1],[22,0],[23,1],[24,2],[25,0],[26,0],[27,1],[28,0]]
>>> print list(add_parent_info(iter(a)))
[[1, 3, 0], [2, 4, 1], [3, 1, 2], [4, 0, 3], [5, 1, 2], [6, 0, 5], [7, 1, 2], [8, 0, 7], [9, 1, 2], [10, 0, 9], [11, 3, 1], [12, 1, 11], [13, 0, 12], [14, 1, 11], [15, 0, 14], [16, 1, 11], [17, 0, 16], [18, 4, 1], [19, 1, 18], [20, 0, 19], [21, 1, 18], [22, 0, 21], [23, 1, 18], [24, 2, 23], [25, 0, 24], [26, 0, 24], [27, 1, 18], [28, 0, 27]]

答案 1 :(得分:1)

编辑:@ WolframH具有生成结构的最佳解决方案。您可以通过将__init__更改为:

,将其优雅的解决方案与Node对象结构相结合
def __init__(self,value,parent_value,child_value,list_of_children):
    self.value=value
    self.parent_value = parent_value
    self.child_value = child_value
    self.children = list_of_children

使用@WolframH生成结构的方法,并遍历调用Node构造函数的所有条目。这样,你可以更容易地插入和删除项目。


您需要定义一个数据结构(想到一棵树)并将值存储在其中。我建议使用树,在树下应用节点类。 (你可以定义一种名为leaf的不同格式,但不会产生任何影响。

class Node(object):
    def __init__(self,val,list_of_children,parent_value)
        self.value = val
        self.children = make_children(list_of_children,val)
        self.parent_value = parent_value

    # in case number of children changes, don't generate middle value
    # until necessary
    def get_list_of_values(self):
        [self.value,len(children),self.parent_value]

    def add_child(self, child):
        self.children.append(child)

    def change_value(self,new_value):
        """update children to reflect changed value"""
        self.value = new_value
        def fn(child):
            child.parent_value = new_value
        map(fn,self.children)


def make_children(list_of_children,parent_value):
    child_list=[]
    # recursive-ish case: there are children
    if list_of_children:
        for child in list_of_children:
            # presuming initially stored as list of lists, e.g. 
            #    [node_value,[node[leaf][leaf]],[node,[node[leaf]],[leaf]]
            #    so child[0] is value, and rest are children
            value = child.pop(0)
            child_list.append(Node(value,child,parent_value)))
        return child_list
    # base case: no children - so return empty set
    else:
        return []

答案 2 :(得分:1)

@WolframH有使用迭代器的最佳解决方案,我将这里作为一个低效的例子而不使用迭代器:D

>>> stuff = [[1,3],[2,4],[3,1],[4,0],[5,1],[6,0],[7,1],[8,0],[9,1],[10,0],[11,3],[12,1],[13,0],[14,1],[15,0],[16,1],[17,0],[18,4],[19,1],[20,0],[21,1],[22,0],[23,1],[24,2],[25,0],[26,0],[27,1],[28,0]]
>>> def walk(parent, i, items):
        item_seq, num_children = items[i]
        children = [items[i]+[parent]]
        for _ in range(num_children):
            i,child = walk(item_seq,i+1,items)
            children.extend(child)
        return i, children

>>> walk(0,0,stuff)[1]
[[1, 3, 0], [2, 4, 1], [3, 1, 2], [4, 0, 3], [5, 1, 2], [6, 0, 5], [7, 1, 2], [8, 0, 7], [9, 1, 2], [10, 0, 9], [11, 3, 1], [12, 1, 11], [13, 0, 12], [14, 1, 11], [15, 0, 14], [16, 1, 11], [17, 0, 16], [18, 4, 1], [19, 1, 18], [20, 0, 19], [21, 1, 18], [22, 0, 21], [23, 1, 18], [24, 2, 23], [25, 0, 24], [26, 0, 24], [27, 1, 18], [28, 0, 27]]