I'm a complete Python novice, although I've written a million lines of 360 Assembler (40 - 50 years ago!). I'm trying to emulate 2-dimensional arrays using Lists. Apparently, I have some basic misunderstanding. I create a 3x3 array and then fill the cells with a simple sequence number (ie, 1 - 9). As I do this, the number properly increments from 1 to 9. However, when I go back and print each cell again, the contents have been altered. What am I missing!
Here is the code (output then follows at the end).
Here is the code (output then follows at the end).
# create a two dimensional array
# create empty grid lists
row = []
col = []
grid = []
# create grid with dimx rows and dimy columns
dimx = 3
dimy = 3
for y in range(0,dimy):
col.append(y)
for x in range(0,dimx):
grid.append(col)
# fill cells with a consecutive number (as a test)
cntr = 1
y = 0
while y < dimy:
x = 0
while x < dimx:
grid[x][y] = cntr
cntr = cntr + 1
print (x,y,grid[x][y])
x = x + 1
y = y + 1
# now go back print contents of each cell
y = 0
while y < dimy:
x = 0
while x < dimx:
print (x,y,grid[x][y])
x = x + 1
y = y + 1---------------------------------- output follows -------------Output:0 0 1
1 0 2
2 0 3
0 1 4
1 1 5
2 1 6
0 2 7
1 2 8
2 2 9
0 0 3
1 0 3
2 0 3
0 1 6
1 1 6
2 1 6
0 2 9
1 2 9
2 2 9
