October 2019
Beginner to intermediate
674 pages
16h 43m
English
Arrays can be sliced using the syntax ar[startIndex: endIndex: stepValue]:
In [82]: ar=2*np.arange(6); ar
Out[82]: array([ 0, 2, 4, 6, 8, 10])
In [85]: ar[1:5:2]
Out[85]: array([2, 6])
Note that if we wish to include the endIndex value, we need to go above it, as follows:
In [86]: ar[1:6:2]
Out[86]: array([ 2, 6, 10])
Obtain the first nelements using ar[:n]:
In [91]: ar[:4]
Out[91]: array([0, 2, 4, 6])
The implicit assumption here is that startIndex=0, step=1.
Start at element 4 and select all the elements till the end:
In [92]: ar[4:]
Out[92]: array([ 8, 10])
Slice array with stepValue=3:
In [94]: ar[::3]
Out[94]: array([0, 6])
To illustrate the scope of indexing in NumPy, let's refer to the following diagram, which ...
Read now
Unlock full access