I have a list of the form
v = [0,0,0,0,0,0,0,0,0] Somewhere in the code I do
vec=v vec[5]=5 and this changes both v and vec:
>>> print vec [0, 0, 0, 0, 0, 5, 0, 0, 0] >>> print v [0, 0, 0, 0, 0, 5, 0, 0, 0] Why does v change at all?
Why does v change at all?
vec and v are both references.
When coding vec = v you assign v address to vec. Therefore changing data in v will also "change" vec.
If you want to have two different arrays use:
vec = list(v)
Because v is pointed to the same list as vec is in memory.
If you do not want to have that you have to make a
from copy import deepcopy vec = deepcopy(v) or
vec = v[:]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With