hello. I am just wondering what is difference between m*[n*[1]] and [n*[1] for k in range(m)]
Because this code:
m,n = 50,50
grid = [n*[1] for k in range(m)]
screen_size =(600,600)
w=float(screen_size[0])/n
h=float(screen_size[1])/m
def setup():
size(screen_size[0],screen_size[1])
noLoop()
def draw():
draw_grid()
def draw_grid():
x,y=0,0
for row in grid:
for ele in row:
if ele == -1:
fill(0)
else:
fill(255)
rect(x,y,w,h)
x+=w
y+=h
x=0
def get_grid_coordinates(x,y):
return int(y/h), int(x/w)
def mousePressed():
coords = get_grid_coordinates(mouseX,mouseY)
pcoords = get_grid_coordinates(pmouseX,pmouseY)
grid[coords[0]][coords[1]],grid[pcoords[0]][pcoords[1]] = -grid[coords[0]][coords[1]],-grid[pcoords[0]][pcoords[1]]
redraw()
works fine (change color of clicked tile in grid) but this code:
m,n = 50,50
grid = m*[n*[1]]
screen_size =(600,600)
w=float(screen_size[0])/n
h=float(screen_size[1])/m
def setup():
size(screen_size[0],screen_size[1])
noLoop()
def draw():
draw_grid()
def draw_grid():
x,y=0,0
for row in grid:
for ele in row:
if ele == -1:
fill(0)
else:
fill(255)
rect(x,y,w,h)
x+=w
y+=h
x=0
def get_grid_coordinates(x,y):
return int(y/h), int(x/w)
def mousePressed():
coords = get_grid_coordinates(mouseX,mouseY)
pcoords = get_grid_coordinates(pmouseX,pmouseY)
grid[coords[0]][coords[1]],grid[pcoords[0]][pcoords[1]] = -grid[coords[0]][coords[1]],-grid[pcoords[0]][pcoords[1]]
redraw()
changes whole columns.
The strangest is that print([n*[1]] for k in range(m) == m*[n*[1]]) writes True.
I am using processing 4.0b8