我正试图解决一个超出我的能力范围的问题。 我有一系列嵌套的列表,当迭代它们时,如果下一个元素是一个列表,我想将其作为当前元素的属性附加。 通常,示例比我的糟糕的英语更好(这里是一些可复制粘贴的代码):
class T(object):
def __init__(self, id, children):
self.id = id
self.children = children or []
def __repr__(self):
return u"T(id={0}, children={1})".format(self.id, self.children)
# first a short example
l0 = [T(id=1, children=[]), T(id=2, children=[]), [T(id=3, children=[]),
T(id=4, children=[]), [T(id=5, children=[])]]]
正如您所看到的,l0有3个元素,最后一个是由三个元素组成的列表:我需要的是将最后一个列表附加到不是列表的前一个元素(并递归执行)
l1 = magic(l0)
[T(id=1, children=[]), T(id=2, children=[T(id=3, children=[]), T(id=4, children=[T(id=5, children=[])])])]
希望有人可以分享一些解决方法,我已经投入了很多小时,但离解决问题还很遥远。
编辑: 为了完整起见,这里提供一个稍微复杂一点的例子。
l0 = [T(children=[], id=1),
T(children=[], id=2),
T(children=[], id=3),
[T(children=[], id=40),
T(children=[], id=41),
T(children=[], id=42),
T(children=[], id=43),
T(children=[], id=44),
T(children=[], id=45),
[T(children=[], id=50),
T(children=[], id=51),
T(children=[], id=52),
T(children=[], id=54),
[T(children=[], id=60),
T(children=[], id=61),
T(children=[], id=62),
T(children=[], id=63),
[T(children=[], id=70)],
T(children=[], id=64)]]],
T(children=[], id=8),
T(children=[], id=9)]
我以@rik-poggi的函数为例构建了一个doctest,目前看来它似乎还可以:
>>> from magic_bag import magic
>>> class T(object):
... def __init__(self, id, children):
... self.id = id
... self.children = children or []
...
... def __repr__(self):
... return u"T(children={0}, id={1})".format(self.children, self.id)
...
>>> l0 = [T(id=1, children=[]), T(id=2, children=[]), T(id=3, children=[]),
... [T(id=40, children=[]), T(id=41, children=[]), T(id=42, children=[]),
... T(id=43, children=[]), T(id=44, children=[]), T(id=45, children=[]),
... [T(id=50, children=[]), T(id=51, children=[]), T(id=52, children=[]),
... T(id=54, children=[]), [T(id=60, children=[]), T(id=61, children=[]),
... T(id=62, children=[]), T(id=63, children=[])]]], T(id=8, children=[]),
... T(id=9, children=[])]
>>> l1 = magic(l0)
>>> l1[0]
T(children=[], id=1)
>>> l1[1]
T(children=[], id=2)
>>> l1[3]
T(children=[], id=8)
>>> l1[4]
T(children=[], id=9)
>>> l1[5]
Traceback (most recent call last):
...
IndexError: list index out of range
>>> l1[2].children[5].children[3]
T(children=[T(children=[], id=60), T(children=[], id=61), T(children=[], id=62), T(children=[], id=63)], id=54)
>>> l0 = [T(id=1, children=[]), T(id=2, children=[]), T(id=3, children=[]),
... [T(id=40, children=[]), T(id=41, children=[]), T(id=42, children=[]),
... T(id=43, children=[]), T(id=44, children=[]), T(id=45, children=[]),
... [T(id=50, children=[]), T(id=51, children=[]), T(id=52, children=[]),
... T(id=54, children=[]), [T(id=60, children=[]), T(id=61, children=[]),
... T(id=62, children=[]), T(id=63, children=[]), [T(id=70, children=[])],
... T(id=64, children=[])]]], T(id=8, children=[]), T(id=9, children=[])]
>>> l1 = magic(l0)
>>> l1[2].children[5].children[0].id
50
>>> len(l1[2].children[5].children[3].children)
5
>>> l1[2].children[5].children[3].children[3].children
[T(children=[], id=70)]
>>> l1[2].children[5].children[3].children[4].id==64
True
使用@rob-wouters的替代方案,它也通过了相同的测试,因此对于我尝试的测试用例,两者都可以正常工作。我会保留Rik的方法,因为我认为独立的函数在需要这种行为的情况下更加方便。
[[1] [2]]
或[[1] 2]
或[1 [2] [3]]
或[1 [2] [3] 4]
或[1 [2 [3]]]
。 - Óscar López