python - 复制列表错误Python(康威生命游戏的一部分)

标签 python list conways-game-of-life copying

这不是我的全部代码。这只是显示了我想要完成的事情。我想要的是列表“板”保持不变,列表"new"改变。我的程序存在的问题是"new"列表和主板都发生了变化。

DEADCELL = "."
LIVECELL = "A"
rows = 5
columns = 5

def startingBoard():
    cellRow = ""
    cellCol = 1
    board = []
    for i in range(rows):
        board.append([DEADCELL]* columns)
    while cellRow != "q":
        cellRow = input("Please enter the row of a cell to turn on (or q to exit): ")  
        if cellRow != "q":
            cellCol = int(input("Please enter a column for that cell: "))
            board[int(cellRow)][cellCol] = LIVECELL
    return board

def printBoard(board):
    for i in range(rows):
        printRow = ""
        for j in range(columns):
            if board[i][j] == LIVECELL:
                printRow = printRow + LIVECELL
            elif board[i][j] == DEADCELL:
                printRow = printRow + DEADCELL
        print(printRow)
    print("\n")

def neighbors(board):
    new = list(board)
    for r in range(rows):
        for c in range(columns):
            neighbors = 0
            # ALL THIS SECTION DOES IS CHECK THE 'NEIGHBORS' AROUND A CELL
            if r - 1 >= 0 and c - 1 >= 0:
                if board[r - 1][c - 1] == LIVECELL:
                    neighbors += 1
            if c - 1 >= 0:
                if board[r][c-1] == LIVECELL:
                    neighbors += 1
            if r + 1 < rows and c + 1 < rows:
                if board[r + 1][c + 1] == LIVECELL:
                    neighbors += 1
            if r - 1 >= 0:
                if board[r - 1][c] == LIVECELL:
                    neighbors += 1
            if r + 1 < rows:  
                if board[r + 1][c] == LIVECELL:
                    neighbors += 1
            if r - 1 >=0 and c + 1 < rows:
                if board[r-1][c+1] == LIVECELL:
                    neighbors += 1
            if c + 1 < rows:
                if board[r][c+1] == LIVECELL:
                    neighbors += 1
            if r + 1 < rows and c - 1 >= 0:
                if board[r+1][c-1] == LIVECELL:
                    neighbors += 1

            # RULES FOR CELLS:
            # IF A LIVE CELL HAS TWO OR THREE NEIGHBORS, IT STAYS ALIVE
            # IF A LIVE CELL HAS LESS THAN TWO NEIGHBORS, IT DIES
            # IF A LIVE CELL HAS MORE THAN THREE NEIGHBORS, IT DIES
            # IF A DEAD CELL HAS THREE NEIGHBORS, IT BECOMES ALIVE
            if board[r][c] == DEADCELL and neighbors == 3:
                new[r][c] = LIVECELL
            elif board[r][c] == LIVECELL and (neighbors < 2 or neighbors > 3):
                new[r][c] = DEADCELL
    #This prints out the new list and the board list to show you guys how they are the same.
    print(new)
    print(board)

def main():
    board = startingBoard()
    printBoard(board)
    neighbors(board)
    printBoard(board)
main()

这是我的输出:

Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 1
Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 2
Please enter the row of a cell to turn on (or q to exit): 2
Please enter a column for that cell: 3
Please enter the row of a cell to turn on (or q to exit): q
.....
.....
.AAA.
.....
.....


[['.', '.', '.', '.', '.'], ['.', '.', 'A', 'A', '.'], ['.', 'A', '.', 'A', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
[['.', '.', '.', '.', '.'], ['.', '.', 'A', 'A', '.'], ['.', 'A', '.', 'A', '.'], ['.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.']]
.....
..AA.
.A.A.
.....
.....

看看 new 列表和 board 列表有何相同。这是为什么?

最佳答案

new = list(board) 是浅复制:它不会克隆子元素,只会复制引用。

a = [[True]]
b = list(a)
b[0][0] = False
a
# => [[False]]

您需要深层复制:

a = [[True]]
from copy import deepcopy
b = deepcopy(a)
b[0][0] = False
a
# => [[True]]

关于python - 复制列表错误Python(康威生命游戏的一部分),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727066/

相关文章:

python - 重复模式,包含的 django url 中的名称不同

python - 如何在 isinstance (object,list) 中打印列表

list - 如何用 nil 创建点对

php - 生命游戏 (PHP)- 不理解我的输出

Java——生命游戏;生命体检测的问题

python - 按键对 dict 列表进行排序。如果缺少键,则假定为连续编号

python - matplotlib - 更快的帧率?

python - 如何从元组中随机选择一个字符串并随机插入文本中的每一行?

java - Collat​​or Locale German - 如何仅在正常字母之后对重音字母进行排序

java - 康威的生命游戏没有按预期进行