arange方法:
j=np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]]) print('j: ',j) print('arange(4): ',np.arange(4)) k=np.array([0,2,0,1]) print('k: ',k) l=j[np.arange(4),k] print('l: ',l) j[np.arange(4),k] += 100 #取出的值每个+100 print('j: ',j) l += 100 print('j: ',j) print('l: ',l)
结果:
j: [[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] arange(4): [0 1 2 3] k: [0 2 0 1] l: [ 1 6 7 11] j: [[101 2 3] [ 4 5 106] [107 8 9] [ 10 111 12]] j: [[101 2 3] [ 4 5 106] [107 8 9] [ 10 111 12]] l: [101 106 107 111]
bool方法:
import numpy as np m=np.array([[1,2,3],[4,5,6],[7,8,9]]) print(m) boolean_array_indexing=(m>5) print(boolean_array_indexing) print(m[m>5])
结果:
[[1 2 3] [4 5 6] [7 8 9]] [[False False False] [False False True] [ True True True]] [6 7 8 9]