arra=np.array([ [1,2,3],
[4,5,6]
[7,8,9] ])
1. Use "," to separate rows and columns
- When there is a comma
For example, arra[1,2] represents the element in the second row and the third column of the index, which is 6
- When there is no comma
For example, a[1] represents the list of elements in the second row of the index, ie [4,5,6]
2. Use ":" to separate the start and end positions
- Get elements in a certain range, but also need to use colons to define the start and end positions
For example, arra[1:3,1:3] represents the elements in the 2-3 rows and 2-3 columns of the index, that is
[[5,6]
[8,9]]
- If the index position starts from 0 (the first row/column), or the index position ends at the last row/column, the start or end position can be omitted
For example, arra has 3 rows and 3 columns, and the highest index number of both rows and columns is 2.
arra[0:2, 2] can be omitted as arra[ :2, 2 ]
arra[2, 0:2] can be omitted as arra[ 2, :2 ]
arra[1:3, 2] can be omitted as arra[ 1: ,2 ]
arra[0:3, 2] can be omitted as arra[ : ,2 ]
3. Discontinuous multi-row index
arra[[0,2]] represents the extraction of elements in rows 1 and 3
arra[:, [0,2]] represents the extraction of the first and third columns