2.3 Numerical Python Arrays
51
In [12]: y[0] = 10.0
In [13]: y
Out[13]: array([ 10., 1., 2.])
# ...as expected
In [14]: x
Out[14]: array([ 10.,
1.,
2.]) # ...x has changed too!
Intuitively, it may seem very strange that changing an element in y causes a similar
change in x! The thing is, however, that our assignment y = x does not make a copy
of the x array. Rather, Python creates another reference, named y, to the same array
object that x refers to. That is, there is one array object with two names (x and y).
Therefore, changing either x or y, simultaneously changes “the other” (note that this
behavior differs from what we found in Sect. 2.2.3 for single integer, float or string
objects).
To really get a copy that is decoupled from the original array, you may use the
copy function from numpy,
In [15]: from numpy import copy
In [16]: x = linspace(0, 2, 3)
# x becomes array([ 0., 1., 2.])
In [17]: y = copy(x)
In [18]: y
Out[18]: array([ 0., 1., 2.])
In [19]: y[0] = 10.0
In [20]: y
Out[20]: array([ 10.,
1.,
2.]) # ...changed
In [21]: x
Out[21]: array([ 0., 1., 2.])
# ...unchanged
2.3.5 Slicing an Array
By use of a colon, you may work with a slice of an array. For example, by writing
x[i:j], we address all elements from index i (inclusive) to j (exclusive) in an
array x. An interactive session illustrates this,
In [1]: from numpy import linspace
In [2]: x = linspace(11, 16, 6)
In [3]: x
Out[3]: array([ 11.,
12.,
13.,
14.,
15.,
16.])
In [4]: y = x[1:5]
In [5]: y
Out[5]: array([ 12., 13., 14., 15.])
51
In [12]: y[0] = 10.0
In [13]: y
Out[13]: array([ 10., 1., 2.])
# ...as expected
In [14]: x
Out[14]: array([ 10.,
1.,
2.]) # ...x has changed too!
Intuitively, it may seem very strange that changing an element in y causes a similar
change in x! The thing is, however, that our assignment y = x does not make a copy
of the x array. Rather, Python creates another reference, named y, to the same array
object that x refers to. That is, there is one array object with two names (x and y).
Therefore, changing either x or y, simultaneously changes “the other” (note that this
behavior differs from what we found in Sect. 2.2.3 for single integer, float or string
objects).
To really get a copy that is decoupled from the original array, you may use the
copy function from numpy,
In [15]: from numpy import copy
In [16]: x = linspace(0, 2, 3)
# x becomes array([ 0., 1., 2.])
In [17]: y = copy(x)
In [18]: y
Out[18]: array([ 0., 1., 2.])
In [19]: y[0] = 10.0
In [20]: y
Out[20]: array([ 10.,
1.,
2.]) # ...changed
In [21]: x
Out[21]: array([ 0., 1., 2.])
# ...unchanged
2.3.5 Slicing an Array
By use of a colon, you may work with a slice of an array. For example, by writing
x[i:j], we address all elements from index i (inclusive) to j (exclusive) in an
array x. An interactive session illustrates this,
In [1]: from numpy import linspace
In [2]: x = linspace(11, 16, 6)
In [3]: x
Out[3]: array([ 11.,
12.,
13.,
14.,
15.,
16.])
In [4]: y = x[1:5]
In [5]: y
Out[5]: array([ 12., 13., 14., 15.])
