python使用数组作为索引遍历数组
觉得有用的话,欢迎一起讨论相互学习~Follow Me
python使用数组作为索引遍历数组
import numpy as np
a=np.arange(0,5)
print(a)
# [0 1 2 3 4]
b=np.arange(0,10).reshape(5,2)
print(b)
# [[0 1]
# [2 3]
# [4 5]
# [6 7]
# [8 9]]
# 将一维数组作为二维数组的索引
c0=b[a][:,0]
print(c0)
# [0 2 4 6 8]
c1=b[a][:,1]
print(c1)
# [1 3 5 7 9]
c2=b[a][0]
print(c2)
# [0 1]
c3=b[a][4]
print(c3)
# [8 9]
c4=b[a][:4]
print(c4)
# [[0 1]
# [2 3]
# [4 5]
# [6 7]]