Selecting array elements

NumPy arrays can have their elements accessed via the [] operator. There are many variants of this operator that we will see throughout this book, but the basic access to array elements is by passing the zero-based offset of the desired element:

In [24]:
   # select 0-based elements 0 and 2
   a1[0], a1[2]

Out[24]:
   (0, 2)

Elements in a two-dimensional array can be used by making use of two values separated by a comma, with the row first and column second:

In [25]:
   # select an element in 2d array at row 1 column 2
   m[1, 2]

Out[25]:
   6

It is possible to retrieve an entire row of a two-dimensional array using just a single value representing the row and omitting the column component:

In [26]:
# all items in row 1
m[1,]

Out[26]:
array([4, 5, 6, 7])

It is possible to retrieve an entire column of a two-dimensional array using the : symbol for the row (just omitting the row value is a syntax error):

In [27]:
   # all items in column 2
   m[:,2]

Out[27]:
   array([ 2,  6, 10, 14, 18])
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.59.198