Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 递归不会发生在类中,除非我使用帮助器方法_Python_Python 3.x - Fatal编程技术网

Python 递归不会发生在类中,除非我使用帮助器方法

Python 递归不会发生在类中,除非我使用帮助器方法,python,python-3.x,Python,Python 3.x,我正在为一个问题编写dfs解决方案 当我按照下面的方式编写代码时,在调试时,我发现 到达self.dfs\u solution\u reward,它不是递归,而是继续执行,导致错误的结果: def dfs_solution_rework(self, end, start= 1, sorted=False, num= None): if not num: for i in range(1, 10): self.dfs_solution_rework(

我正在为一个问题编写dfs解决方案

当我按照下面的方式编写代码时,在调试时,我发现 到达self.dfs\u solution\u reward,它不是递归,而是继续执行,导致错误的结果:

def dfs_solution_rework(self, end, start= 1, sorted=False, num= None):
    if not num:
        for i in range(1, 10):
            self.dfs_solution_rework(end, start, num=i)
    elif num <= end:
        if num >= start:
            yield num
        last_digit = num % 10
        if not (last_digit == 0 or last_digit == 9):
            self.dfs_solution_rework(end, start, num=num * 10 + (last_digit - 1))
            self.dfs_solution_rework(end, start, num=num * 10 + (last_digit + 1))
        elif last_digit == 0:
            self.dfs_solution_rework(end, start, num=num * 10 + 1)
        else:
            self.dfs_solution_rework(end, start, num=num * 10 + 8)
另一方面,如果我使用util helper方法编写dfs,如下所示,那么它可以正常工作

def dfs_solution(self, end, start= 1, sorted=False, num= None):
    def dfs_util(end, start, num):
        if num <= end:
            if num >= start:
                print(num)
            last_digit = num % 10
            if not (last_digit == 0 or last_digit == 9):
                dfs_util(end, start, num=num * 10 + (last_digit - 1))
                dfs_util(end, start, num=num * 10 + (last_digit + 1))
            elif last_digit == 0:
                dfs_util(end, start, num=num * 10 + 1)
            else:
                dfs_util(end, start, num=num * 10 + 8)

    for i in range(1, 10):
        dfs_util(end, start, num=i)
关于为什么会发生这种行为有什么帮助吗?我已经在VS代码中调试了它,以理解它,但没有任何想法

附:不是作业问题


感谢

递归生成器需要生成递归的结果

与您的问题相同的简化示例是此递归计数器:

def count_to_zero(n):
    yield n
    if n == 0:
        return
    count_to_zero(n-1)
它只产生一个值:n。这是因为对count_to_zeron-1的递归调用刚刚创建了一个从未使用过的生成器

测试:

这里的解决办法是:

def count_to_zero(n):
    yield n
    if n == 0:
        return
    yield from count_to_zero(n-1)
测试:

在您的示例中也需要这样做,对于self.dfs\u solution\u rework的每个递归调用,例如

Python 2 请注意,这个语法在Python2中不起作用。我们必须这样做:

for result in count_to_zero(n-1):
    yield result
这与:

yield from count_to_zero(n-1)

产量和印刷品不一样。我也用产量测试过。没有粘贴代码,因为我正在调试并更新了代码。但是,本质上,我用print测试了它,以及printnum行的两个函数中的yield,每次使用yield而不是print时,都没有输出,是吗?谢谢@Aran Fey,由于您提供的提示,我能够理解代码中的错误。作为将来的参考,问题是递归代码正在生成值,而我不会生成这些值。在你的提示下,我检查并解决了这个问题。谢谢
 yield from self.dfs_solution_rework(end, start, num=num * 10 + 1)
for result in count_to_zero(n-1):
    yield result
yield from count_to_zero(n-1)