Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.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中使用for循环更改二维列表_Python_List - Fatal编程技术网

在python中使用for循环更改二维列表

在python中使用for循环更改二维列表,python,list,Python,List,我正在尝试用python制作康威的《生活游戏》,但我得到了奇怪的输出。打印语句用于调试目的 def dead_state(width, height): #build a board that is size width x height board = [] for col in range(width): board.append(0) state = [board] for row in range(height - 1

我正在尝试用python制作康威的《生活游戏》,但我得到了奇怪的输出。打印语句用于调试目的

def dead_state(width, height):
     #build a board that is size width x height
     board = []
     for col in range(width):
         board.append(0)
     state = [board]

     for row in range(height - 1):
         state.append(board)

     return state


def random_state(width, height):
    #build board with dead_state
    board = dead_state(width, height)

    #randomize board
    print("before randomizing: " + str(board)) 

    for row in range(height):

        for col in range(width):
            print("board[" + str(row) + "] before: " + str(board[row][col]))
            if random.random() > 0.5:
                board[row][col] = 1
            else:
                board[row][col] = 0
         print("board[" + str(row) + "][" + str(col) + "] after: " + str(board[row][col]))



    print("board after randomizing: " + str(board))

    return board
运行时,它会随机化一行中的索引,但随后会使每一行都相同

一个输出:

board after randomizing: 
[
  [0, 0, 1, 0, 0, 1, 0], 
  [0, 0, 1, 0, 0, 1, 0],
  [0, 0, 1, 0, 0, 1, 0],
  [0, 0, 1, 0, 0, 1, 0],
  [0, 0, 1, 0, 0, 1, 0]
]

问题很可能在于创建董事会的死区代码。很有可能是您使用乘法创建了n个列表。这将创建一个列表,然后在外部列表中引用n个。我们可以通过一个简单的例子看到这一点。我们创建一组内部列表,每个列表有3个空字符串。然后,我们更新第一个列表中第二个项目的值,将其设置为1,但正如您所看到的,它会更新所有列表的值。这是因为它们是我们创建嵌套列表的方式

my_list=[['','']]*4
打印(我的清单)
我的列表[0][1]=1
打印(我的清单)
输出

[['', '', ''], ['', '', ''], ['', '', ''], ['', '', '']]
[['', 1, ''], ['', 1, ''], ['', 1, ''], ['', 1, '']]
[['', '', ''], ['', '', ''], ['', '', ''], ['', '', '']]
[['', 1, ''], ['', '', ''], ['', '', ''], ['', '', '']]
相反,我们应该使用列表理解和范围创建嵌套列表。这将在内存中创建4个单独的列表,每个列表都是唯一的,更改列表1中的元素2只会更改该列表

my_list=[['','',表示范围(4)内的u)
打印(我的清单)
我的列表[0][1]=1
打印(我的清单)
输出

[['', '', ''], ['', '', ''], ['', '', ''], ['', '', '']]
[['', 1, ''], ['', 1, ''], ['', 1, ''], ['', 1, '']]
[['', '', ''], ['', '', ''], ['', '', ''], ['', '', '']]
[['', 1, ''], ['', '', ''], ['', '', ''], ['', '', '']]

显示生成2d列表的死区状态函数。您可能已经使用*like
[]*4
创建了列表,这将创建4个引用,所有引用都指向同一列表,因此更改其中一个引用将更改其他引用,因为它们都指向同一列表。您必须提供。
dead\u state
函数在做什么?谢谢!成功了!我使用了列表comp。在此之前,我只是将0添加到列表,然后将该列表添加到列表列表。这更容易理解。