
上QQ阅读APP看书,第一时间看更新
Selecting NumPy array elements
From time to time, we will wish to select a specific constituent of an array. We will take a look at how to do this, but to kick off, let's make a 2x2 matrix again:
In: a = np.array([[1,2],[3,4]]) In: a Out: array([[1, 2], [3, 4]])
The matrix was made this time by giving the array()
function a list of lists. We will now choose each item of the matrix one at a time, as shown in the following code snippet. Recall that the index numbers begin from 0
:
In: a[0,0] Out: 1 In: a[0,1] Out: 2 In: a[1,0] Out: 3 In: a[1,1] Out: 4
As you can see, choosing elements of an array is fairly simple. For the array a
, we just employ the notation a[m,n]
, where m
and n
are the indices of the item in the array. Have a look at the following figure for your reference:
