python - Values of variables inside a for-loop -
python - Values of variables inside a for-loop -
i have array a
defined outside for-loop. b
variable assigned equal a
within loop. alter values of b
within loop alters a
. why/how happen?
>>> import numpy np >>> = np.asarray(range(10)) >>> in range(5,7): b = #assign b equal b[b<i]=0 #alter b b[b>=i]=1 print
output:
[0 0 0 0 0 1 1 1 1 1] #unexpected!! [0 0 0 0 0 0 0 0 0 0]
why a
altered when don't explicitly so?
because when b = a
reference gets copied. both a
, b
point same object.
if want create re-create of a
need example:
import re-create ... b = copy.deepcopy(a)
python python-2.7 numpy
Comments
Post a Comment