I am a beginner at python and I don't want these arrays to copy each other but they are automatically:
a = numpy.zeros(4)
b = a
a[1] = 10
print b[1]
and it returns 10 instead of 0. How do I disconnect these two arrays?
解决方案
"The arrays are automatically copying each other" is a false statement for several reasons. The primary reason is that you only have one array, and two variables names that refer to that array.
Here are three ways to copy a numpy array (i.e. create another array exactly like it):
>>> a = numpy.zeros(4)
>>> b = a.copy()
>>> c = numpy.copy(a)
>>> d = numpy.array(a)
>>> a[1] = 10
>>> a
array([ 0., 10., 0., 0.])
>>> b
array([ 0., 0., 0., 0.])
>>> c
array([ 0., 0., 0., 0.])
>>> d
array([ 0., 0., 0., 0.])
Note that slice-copying (e.g. e = a[:]) will not work with numpy arrays.