数组索引
import numpy as np
a = np.array(
[[1,2,3],
[4,5,6],
[7,8,9]])
print(a[:,2])
print(a[:,[2,1]])
print(a[[2,1],[2,1]])
print(a[[2,1]],'\n')
t = (2,1)
n1 = np.array(2)
n2 = np.array([2,1])
print('t',a[t])
print(n1.shape)
print(a[n1])
print(n2.shape)
print(a[n2])
输出为:
[3 6 9]
[[3 2]
[6 5]
[9 8]]
[9 5]
[[7 8 9]
[4 5 6]]
t 8
()
[7 8 9]
(2,)
[[7 8 9]
[4 5 6]]
只针对numpy数组,列表索引只支持整数或者切片(:)。