I encountered a problem with generating a grid of integers in Python. My code:
c = [[0] * 3] * 3
print c
c[0][0] = 1
print c
The first print
statement prints as follows, which is the grid I want
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
The second print
statement prints as follows, which is not what I want. I just wanted to change grid[0][0]
to 1, but not every head of the lists in grid.
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
How do I generate a grid in a quick way, without this kind of modification issue?
Thank you!