【问题标题】:Recursive stack in PythonPython中的递归堆栈
【发布时间】:2021-01-21 06:34:07
【问题描述】:

我正在尝试了解以下 python 递归函数的调用堆栈。该函数给出给定集合的所有组合。

def subsets(A):
    if not A:
        yield []
    else:
        for s in subsets(A[1:]):
            yield s
            yield [A[0]] + s
l1=[1,2,3]
print(list(subsets(l1)))

程序正在根据需要生成输出:

[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]

但是我无法可视化调用堆栈。如何打印[1,2,3]? 据我了解如下

call stack 1 : values are : for s in [2,3], a[0] = 1, a = [1,2,3]
call stack 2 : values are : for s in [3], a[0] = 2, a = [2,3]
call stack 3 : values are : for s in [], a[0] = 3, a = [3]

可以帮我理解sa 值的调用堆栈流程吗?

【问题讨论】:

标签: python recursion callstack


【解决方案1】:

当您最初调用susbsets 时,参数A[1, 2, 3]。需要注意的重要一点是,在开始为当前参数[1, 2, 3] 生成值之前,该函数将使用参数[2, 3][3][] 重复调用自身。然后所有这些递归调用都返回,我们执行以下代码,A 的值为[1, 2, 3]

for s in subsets(A[1:]):
    yield s # produces [2, 3]
    yield [A[0]] + s # produces [1, 2, 3]

所以我们希望最后两个值在包含列表中是[2, 3], [1, 2, 3]

【讨论】:

    【解决方案2】:

    您在生成器上调用list()。这将一次又一次地调用生成器,直到它耗尽。让我们跟踪执行流程。这是理解生成器的一个很好的练习。我会将所有内容格式化为代码块,以便我可以使用适当的缩进来阐明生成器调用的层次结构。

    subsets([1, 2, 3]) is called, 
        so A is [1, 2, 3]. 
        This list is not empty, so the else block is executed.
        A[1:] is [2, 3], so to determine the first s,
        subsets([2, 3]) is called.
            Now A is [2, 3], so A[1:] is [3], so to determine s,
            subsets([3]) is called.
                Now A is [3], so A[1:] is [], so to determine s,
                subsets([]) is called.
                    Now A is [], so the if block is executed.
                    This yields [].
                The loop starts with s = [].
                This yields [] again.
            Now this loop starts, also with s = [],
            because this is what subsets([3]) has just yielded.
            So this yields [] as well.
        So subsets([2, 3]) has yielded [], 
        so this loop also starts with s = [].
        This yields [] yet again.
    So subsets([1, 2, 3]) has yielded [], 
    and now this generator is called again (because of list()), 
        picking up the action after the previously executed yield statement.
        So we reach the next statement: yield [A[0]] + s.
        This yields [1].
    subsets([1, 2, 3]) is called again,
        picking up at the end of the first run through the for loop,
        so to determine the next s, 
        subsets([2, 3]) is called again, 
            picking up at yield [A[0]] + s.
            This yields [2].
        So the loop starts again, with s = [2].
        This yields [2].
    subsets([1, 2, 3]) is called again,
        picking up at yield [A[0]] + s, with s = [2].
        This yields [1, 2].
    subsets([1, 2, 3]) is called again,
        picking up at the end of the for loop,
        so to determine the next s, 
        subsets([2, 3]) is called again,
            picking up at the end of the for loop,
            so to determine the next s,
            subsets([3]) is called again, 
                picking up at yield [A[0]] + s.
                This yields [3].
            So the loop starts again, with s = [3].
            This yields [3].
        So the loop starts again, with s = [3].
        This yields [3].
    subsets([1, 2, 3]) is called again,
        picking up at yield [A[0]] + s, with s = [3].
        This yields [1, 3].
    subsets([1, 2, 3]) is called again,
        picking up at the end of the for loop,
        so to determine the next s, 
        subsets([2, 3]) is called again,
            picking up at yield [A[0]] + s, with s = [3].
            This yields [2, 3].
        So the loop starts again, with s = [2, 3].
        This yields [2, 3]. 
    subsets([1, 2, 3]) is called again,
        picking up at yield [A[0]] + s, with s = [2, 3].
        This yields [1, 2, 3].
    subsets([1, 2, 3]) is called again,
        picking up at the end of the for loop,
        so to determine the next s, 
        subsets([2, 3]) is called again,
            picking up at the end of the for loop,
            so to determine the next s, 
            subsets([3]) is called again, 
                picking up at the end of the for loop,
                so to determine the next s, 
                subsets([]) is called again, 
                    picking up at the end of the if block,
                    so we reach the end of the generator, 
                    which means it is exhausted and yields nothing anymore.
                So there is no further iteration of the for loop,
                hence subsets([3]) is also exhausted.
            So subsets([2, 3]) is also exhausted.
        So subsets([1, 2, 3]) is also exhausted.
    

    【讨论】:

      【解决方案3】:

      这是 powerset 的mathematical definition 的直接实现

      空的 A 没有子集 (yield [])。

      对于非空,你有两种选择

      1. 保留第一个元素,并连接其余所有可能的子集(yield [A[0]] + s)

      2. 不要保留第一个元素,并返回其余元素的可能子集 (yield s)

      所以,A = [1, 2, 3][1] + subsets([2, 3])subsets([2, 3]) 的联合。同样,subsets([2, 3])[2] + subsets([3])subsets([3]) 的并集。最后,subsets([3])[][3]。这给出了 3 个步骤,每个步骤有 2 个可能的结果,给出了 8 种可能的组合。

      【讨论】:

        猜你喜欢
        • 2017-11-27
        • 2015-09-12
        • 1970-01-01
        • 2018-10-16
        • 2011-11-24
        • 2015-06-06
        • 2011-01-30
        • 2021-11-17
        • 1970-01-01
        相关资源
        最近更新 更多