Python 2D列表计算不正确

Python 2D列表计算不正确,python,arrays,list,Python,Arrays,List,我想做一个人生游戏。有一个奇怪的bug我真的无法修复,因为我真的不知道问题出在哪里?我猜它在循环中?我真的不知道。我尝试使用if total>0打印(total)来调试它,当它应该是3时,总数只有2。我很抱歉,如果我解释得很混乱,因为我也很困惑 def test(): board = [[0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 1, 0,

我想做一个人生游戏。有一个奇怪的bug我真的无法修复,因为我真的不知道问题出在哪里?我猜它在循环中?我真的不知道。我尝试使用if total>0打印(total)来调试它,当它应该是3时,总数只有2。我很抱歉,如果我解释得很混乱,因为我也很困惑

def test():
    board = [[0, 0, 0, 0, 0],
             [0, 0, 1, 0, 0],
             [0, 0, 1, 0, 0],
             [0, 0, 1, 0, 0],
             [0, 0, 0, 0, 0]]

    #Tracking the neighbor, it shows that there is 3 alive neighbors in 
    #here.
    print(board[2][1])
    print(board[2-1][1+1])
    print(board[2][1+1])
    print(board[2+1][1+1])

    return board

def update(grid, N):
    newGrid = grid.copy()
    for i in range(N):
        if i == 0 or i == 4:
            continue
        for j in range(N):
            if j == 0 or j == 4:
                continue
            total = 0
            total = total + grid[i][j-1] #
            total = total + grid[i][j+1] #
            total = total + grid[i-1][j] #
            total = total + grid[i+1][j] #
            total = total + grid[i-1][j-1] #
            total = total + grid[i-1][j+1] #
            total = total + grid[i+1][j-1] #
            total = total + grid[i+1][j+1] #

            # In here it only states that there's only 2 alive neighbors 
            # when there should've been 3
            if total > 0:
                print(total)

            # apply Conway's rules 
            if grid[i][j] == 1: 
                if (total < 2) or (total > 3): 
                    newGrid[i][j] = 0 
                elif total == 3:
                    newGrid[i][j] = 1
            else:
                if total == 3: 
                    newGrid[i][j] = 1

    grid[:] = newGrid[:]
    return(grid)

f = 0

zboard = test()
while f <= 3:
    print("Generation: " + str(f))
    gen = update(zboard, 5)
    for i in gen:
        print(i)
        f += 1
def test():
线路板=[[0,0,0,0,0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0]]
#跟踪邻居,它显示有3个活着的邻居
#在这里。
打印(电路板[2][1])
打印(板[2-1][1+1])
打印(板[2][1+1])
打印(板[2+1][1+1])
返回板
def更新(网格,N):
newGrid=grid.copy()
对于范围(N)中的i:
如果i==0或i==4:
持续
对于范围(N)内的j:
如果j==0或j==4:
持续
总数=0
总计=总计+网格[i][j-1]#
总计=总计+网格[i][j+1]#
总计=总计+网格[i-1][j]#
总计=总计+网格[i+1][j]#
总计=总计+网格[i-1][j-1]#
总计=总计+网格[i-1][j+1]#
总计=总计+网格[i+1][j-1]#
总计=总计+网格[i+1][j+1]#
#这里只说只有两个活着的邻居
#本来应该有三个的
如果总数>0:
打印(总计)
#应用康威规则
如果网格[i][j]==1:
如果(总数<2)或(总数>3):
newGrid[i][j]=0
elif总计==3:
newGrid[i][j]=1
其他:
如果总数=3:
newGrid[i][j]=1
网格[:]=新网格[:]
返回(网格)
f=0
zboard=测试()

虽然f如果你像你发布的那样运行代码,你会犯一个错误,因为你没有在你的函数
def test():
之后缩进行,直到
返回你需要使用的板

执行
newGrid=grid.copy()
操作时,由于您有一个二维列表,
newGrid
中的子列表将不会独立于
grid
中的子列表。您的问题是,
grid
在每次更新
newGrid
时都会更新

您需要替换此:

newGrid = grid.copy()
据此:

import copy
newGrid = copy.deepcopy(grid)
这里有一个例子向你展示发生了什么
cop_1
为从属副本,而cop_2为独立副本:

board = [[0, 0],
        [0, 0]]
cop_1 = board.copy()

import copy
cop_2 = copy.deepcopy(board)

board[0][0] = 3 # change a value of board

print("cop_1[0][0] =", cop_1[0][0])
# cop_1[0][0] = 3
print("cop_2[0][0] =", cop_2[0][0])
# cop_2[0][0] = 0

你能具体说明你的预期和实际结果吗?我文件中的缩进是正确的,我将编辑邮递。只是想让您知道我是如何找到问题的原因的:我看到如果total==3,那么条件
从未达到,这很奇怪,我还看到在for循环期间,您板中的值
1
被更新为
0
。然后我推断你有依赖列表