Indexing and slicing arrays
There are two basic methods to access the data in a NumPy array; let's call that array for A
. Both methods use the same syntax, A[obj]
, where obj
is a Python object that performs the selection. We are already familiar with the first method of accessing a single element. The second method is the subject of this section, namely slicing. This concept is exactly what makes NumPy and SciPy so incredibly easy to manage.
The basic slice method is a Python object of the form slice(start,stop,step)
, or in a more compact notation, start:stop:step
. Initially, the three variables, start
, stop
, and step
are non-negative integer values, with start
less than or equal to stop
.
This represents the sequence of indices k = start + (i * step), where k runs from start
to the largest integer k_max = start + step*int((stop-start)/step), or i from 0
to the largest integer equal to int((stop - start) / step). When a slice method is invoked on any of the dimensions of ndarray
, it...