首页 > 解决方案 > 尽管没有返回列表,但用作参数的列表正在更改

问题描述

def main():

startingBoard = [
    [5,3,0,0,7,0,0,0,0],
    [6,0,0,1,9,5,0,0,0],
    [0,9,8,0,0,0,0,6,0],
    [8,0,0,0,6,0,0,0,3],
    [4,0,0,8,0,3,0,0,1],
    [7,0,0,0,2,0,0,0,6],
    [0,6,0,0,0,0,2,8,0],
    [0,0,0,4,1,9,0,0,5],
    [0,0,0,0,8,0,0,7,9]
]

printBoard(startingBoard)
print("^^ This is the startingBoard")

index = solveRow(startingBoard, 0)

printBoard(startingBoard)
print("^^ This should still be the startingBoard but has changed despite no return list")

if index < 10:
    solveColumn(startingBoard, index)

printBoard(startingBoard)
print("^^ This should also print startingBoard but shows the changes of both functions")



def solveRow(board, row):

for index, num in enumerate(board[row]):
    column = []
    for r in board:
        column.append(r[index])
    for i in range(9):
        if num == 0:
            if i+1 not in board[row] and i+1 not in column:
                board[row][index] = i+1


if 0 in board[row]:
    index = board[row].index(0)
    return index
return 10

def solveColumn(board, colIndex):
column = []
for r in board:
    column.append(r[colIndex])

for rowNum, row in enumerate(board):
    for index, num in enumerate(column):
            for i in range(9):
                if num == 0:
                    if i+1 not in column and i+1 not in board[rowNum]:
                        column[index] = i+1

for i, row in enumerate(board):
    row[colIndex] = column[i]

return board

def printBoard(board):
print("       ---SUDOKU BOARD--- ")
print()
print("    " + "1  2  3  4  5  6  7  8  9")
print("   ---------------------------")
for i in range(9):
    print(" " + str(i+1) + " " + str(board[i]) + " " + str(i+1))
print("   ---------------------------")
print("    " + "1  2  3  4  5  6  7  8  9")
print()
print()


if __name__ == "__main__":
main()

这是输出:

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

^^ 这是起跑板

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

^^ 这应该仍然是起始板,但尽管没有返回列表,但已经改变

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

^^ 这也应该打印startingBoard,但显示两个函数的变化

本质上,我使用列表作为函数的参数,但如果该函数的逻辑发生某些事情,我想在另一个函数中使用原始列表。问题是尽管没有返回参数列表,但列表会通过每个函数进行更改。

我想这是一个愚蠢的错误。可能是全局变量或将原始变量设为元组,然后可以将其转移到工作列表进行检查。只是寻求帮助。谢谢。

我是 python 新手,我想我会尝试使用数独求解器。请不要在逻辑上提供帮助,因为我想自己解决这个问题,然后与更有效的方法进行比较来改进我的 Python。对不起,我正在学习混乱的代码,通常在解决所有问题后清理它。谢谢你的帮助。

标签: pythonpython-3.xlist

解决方案


默认情况下,对象在 Python 中通过引用传递。

因此,在solveRow您更改时board,您实际上是在更改您通过的同一个板对象。

def solveRow(board, row):

  for index, num in enumerate(board[row]):
    column = []
    for r in board:
        column.append(r[index])
    for i in range(9):
        if num == 0:
            if i+1 not in board[row] and i+1 not in column:
                board[row][index] = i+1

您可以查看创建列表对象副本的copy()和方法,以避免对原始对象进行不必要的覆盖。deepcopy()


推荐阅读