【发布时间】:2017-08-16 16:08:45
【问题描述】:
我想解散以下函数的递归,因为某些输入数据导致超出递归深度错误。 从长远来看,Increasing the recursion depth 不是解决方案。
def foo(x):
for element in x.elements:
element.depth = x.depth + 1
self.foo(element)
List flattening 不适用,因为需要保留原始列表结构。
This solution 使用生成器,但包含递归。它是一个生成器这一事实有什么不同吗?
最后是stack approach。在这里,我不确定这是否 100% 适用,因为要分配的值相互依赖。
什么是优雅的(Pythonic)非递归解决方案?
【问题讨论】:
标签: python algorithm list nested non-recursive