简体   繁体   English

Python列表返回“无”

[英]Python list return 'None'

Code: 码:

puzzle1= [
          [7,0,0,0,0,0,2,1,8],
          [0,4,8,6,2,9,0,0,0],
          [0,0,3,0,0,1,0,0,0],
          [0,0,7,0,0,8,0,3,2],
          [0,0,9,7,0,6,5,0,0],
          [6,8,0,1,0,0,7,0,0],
          [0,0,0,2,0,0,4,0,0],
          [0,0,0,4,1,5,8,7,0],
          [3,5,4,0,0,0,0,0,6]
          ]  
def eliminate_values(puzzle):
    redo = False
    for i in range(9):
        for j in range(9):
            if puzzle[i][j]==0 or isinstance(puzzle[i][j], list):
                puzzle[i][j] = []
                for num in range(1,10):
                   num_check = True;
                   for x in range(9):
                       if puzzle[i][x]==num:
                           num_check = False
                       if puzzle[x][j]==num:
                           num_check = False
                       if i<3:
                           aa=0
                       elif i<6 and i>2:
                           aa=3
                       else:
                           aa=6
                       if j<3:
                           bb=0
                       elif j<6 and j>2:
                           bb=3
                       else:
                           bb=6
                       for a in range(3):
                           for b in range(3):
                               if puzzle[a+aa][b+bb]==num:
                                   num_check = False
                   if num_check:
                       puzzle[i][j].append(num)
                if len(puzzle[i][j]) == 1:
                    puzzle[i][j] = puzzle[i][j][0]
                    redo = True;
    if redo:
        eliminate_values(puzzle)
    else:
        print(puzzle)
        return puzzle

puzzle=eliminate_values(puzzle1)
print(puzzle)

Console: 安慰:

[[7, 9, 6, 3, 5, 4, 2, 1, 8], [1, 4, 8, 6, 2, 9, 3, 5, 7], [5, 2, 3, 8, 7, 1, 9, 6, 4], [4, 1, 7, 5, 9, 8, 6, 3, 2], [2, 3, 9, 7, 4, 6, 5, 8, 1], [6, 8, 5, 1, 3, 2, 7, 4, 9], [8, 7, 1, 2, 6, 3, 4, 9, 5], [9, 6, 2, 4, 1, 5, 8, 7, 3], [3, 5, 4, 9, 8, 7, 1, 2, 6]]
None

Comments: 评论:

I am new to python but i dont understand why print IS working within the function and NOT after it is returned to the main program. 我是python的新手,但是我不明白为什么print在函数内起作用,而不是在返回主程序后起作用。 (expecting it to print twice but only prints once then 'None') (期望它打印两次,但是只打印一次,然后“无”)

@tobias_k is right. @tobias_k是正确的。

In every recursive function you have a base case and a recursive case. 在每个递归函数中,都有基本情况和递归情况。 The base case is when you reach the end of your recursion and return the final value from your recursive function. 基本情况是到达递归末尾并从递归函数返回最终值时。 The recursive case is where the function calls itself again. 递归情况是函数再次调用自身。

You need to be returning in both cases though. 不过,在两种情况下都需要返回。

If you don't, then even if you're eventually hitting your base case, the return value of the base case doesn't get passed up the stack. 如果您不这样做,那么即使您最终遇到了基本情况,基本情况的返回值也不会传递到堆栈中。

ie: 即:

def recursiveDecrement(x):
    if x > 0:
        print("Recursive case. x = %s" %x)
        recursiveDecrement(x - 1)
        print("I should have returned...x = %s" %x)
    else:
        print("Base case. x = %s" %x)
        return x

If I call recursiveDecrement(5) my output would be: 如果我调用recursiveDecrement(5)我的输出将是:

Recursive case. x = 5
Recursive case. x = 4
Recursive case. x = 3
Recursive case. x = 2
Recursive case. x = 1
Base case. x = 0
I should have returned...x = 1
I should have returned...x = 2
I should have returned...x = 3
I should have returned...x = 4
I should have returned...x = 5

However, once the base case is hit, the method just continues to execute and at the end nothing is returned and x is still equal to 5. 但是,一旦遇到基本情况,该方法将继续执行,最后不返回任何内容,并且x仍等于5。

Change your if statement to return in both cases and everything should work. 更改if语句以在两种情况下都返回,并且一切正常。

if redo:
    return eliminate_values(puzzle)
else:
    return puzzle

If redo is True , you are recursively calling your function, and somewhere down the stack, once redo is False , you print and return the result. 如果redoTrue ,则递归地调用函数,然后在堆栈的某个地方,一旦redoFalse ,则打印并返回结果。 However, this result is not propagated up the call stack, thus the outermost function call will return nothing, ie None , which is then printed. 但是,此结果不会在调用堆栈中传播,因此最外面的函数调用将不返回任何内容,即None ,然后将其打印出来。 For this, you also have to return the result of the recursive call: 为此,还必须return递归调用的结果:

    if redo:
        return eliminate_values(puzzle)  # try again and return result
    else:
        return puzzle  # result found in this try, return it

Alternatively, instead of using recursion, you could wrap your function body in a while loop. 另外,也可以不使用递归,而可以将函数体包装在while循环中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM