我正试图解决一个问题,不幸的是,这个问题超出了我的能力范围。 我有一系列嵌套列表,并在迭代它们时,如果下一个元素是一个列表,我想将它作为当前元素的属性追加。 像往常一样,一个例子比我糟糕的英语(这里有一些代码可以复制和粘贴)更好:
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的,因为我认为当我需要这种行为时,一个独立的函数可以更方便。
答案 0 :(得分:3)
我就是这样做的:
class T(object):
def __init__(self, id, children):
self.id = id
self.children = children or []
def add_children(self, children):
for child in children:
if isinstance(child, list):
self.children[-1].add_children(child)
else:
self.children.append(child)
def __repr__(self):
return u"T(id={0}, children={1})".format(self.id, self.children)
l0 = [T(id=1, children=[]),
T(id=2, children=[]), [T(id=3, children=[]), T(id=4, children=[]),
[T(id=5, children=[])]]]
root = T(id=0, children=[])
root.add_children(l0)
print(root.children)
如果您真的想要一个独立的方法,而不是使用两个处理相同案例的函数,您可以使用以下方法:
def add_children(node, children):
for child in children:
if hasattr(child, '__iter__'):
add_children(node.children[-1], child)
else:
node.children.append(child)
def create_tree(lst):
root = T(id=0, children=[])
add_children(root, lst)
return root.children
print(create_tree(l0))
这有点优雅,因为与两个几乎相同的函数相比,它避免了大量的重复代码。我确实改变了我的isinstance
支票,支持检查__iter__
,这样可以更灵活地存储您的孩子名单。
答案 1 :(得分:1)
我想出了这个:
def append_children(parent, iterable):
last = None
for i in iterable:
if hasattr(i, '__iter__'):
append_children(last, i)
else:
parent.children.append(i)
last = i
def magic(lst):
result = []
for i in lst:
if hasattr(i, '__iter__'):
append_children(result[-1], i)
else:
result.append(i)
return result
示例:
>>> l_in = [T(id=1, children=[]), T(id=2, children=[]), [T(id=3, children=[]),
... T(id=4, children=[]), [T(id=5, children=[])]]]
>>> l_expected = [T(id=1, children=[]),
... T(id=2, children=[T(id=3, children=[]),
... T(id=4, children=[T(id=5, children=[])])])]
>>> l_ouput = magic(l_in)
>>> repr(l_output) == repr(l_expected)
True