Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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_List_Conways Game Of Life_Copying - Fatal编程技术网

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

复制列表错误Python(康威生活游戏的一部分),python,list,conways-game-of-life,copying,Python,List,Conways Game Of Life,Copying,这不是我的全部代码。这正好说明我想做什么。我想要的是名单“董事会”保持不变,名单“新”改变。这个程序的问题是“新”列表和电路板都会发生变化 DEADCELL = "." LIVECELL = "A" rows = 5 columns = 5 def startingBoard(): cellRow = "" cellCol = 1 board = [] for i in range(rows): board.append([DEADCELL]*

这不是我的全部代码。这正好说明我想做什么。我想要的是名单“董事会”保持不变,名单“新”改变。这个程序的问题是“新”列表和电路板都会发生变化

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()
查看新列表和电路板的相同之处。这是为什么?

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]]

对于我的项目,我们不允许导入任何内容。还有其他方法可以进行深度复制吗?
b=[a中x的列表(x)]
应该可以。谢谢大家的反馈!
a = [[True]]
from copy import deepcopy
b = deepcopy(a)
b[0][0] = False
a
# => [[True]]