I'm new to Python and the following toy problem baffles me:
import numpy as np
V = np.ones([3,3])
Vini = V
Niter = 10
Vlist = list() # collect all iterations
Vlist.append(V)
for it in range(1, Niter-1):
for ix in range(0,3):
for iy in range(0,3):
V[iy,ix] = Vlist[it-1][iy,ix] + 1
Vlist.append(V) # save current iteration
(note that I am not after efficiency improvements – this is purely for didactic purposes)
My issue is that all elements of Vlist are identical, where I expected them to differ (by 1) at each iteration. Even Vini has changed its value! Is this a shallow/deep copy kind of thing, or something else? How do I fix this, and how do I reason about this kind of thing? (coming from other languages where a=b means deep copy).