import numpy as np
01一维数组
一维数组的 索引和切片
a = np.arange(9)
3-7 不包括7
b = a[3:7]
print(b)
0-7步长为2
c = a[:7:2]
print(c)
下标翻转数组
d = a[::-1]
print(d)
# 输出结果
[3 4 5 6]
[0 2 4 6]
[8 7 6 5 4 3 2 1 0]
02多维数组的切片和索引
reshape 改变数组的纬度,参数为正整数元组
a = np.arange(24).reshape(2,3,4)
print(a)
# 输出结果
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
多维数组的切片
b = a[0,0,0]
print(b)
输出结果
0
c = a[:,0,0]
print(c)
输出结果
[ 0 12]
d = a[0]
print(d)
输出结果
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
e = a[0,:,:] #等价于e = a[0,...] 多个冒号等价于...
print(e)
输出结果
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
f = a[0,1]
print(f)
输出结果
[4 5 6 7]
g = a[0,1,::2]
print(g)
输出结果
[4 6]
h = a[...,1]
print(h)
输出结果
[[ 1 5 9]
[13 17 21]]
i = a[:,1]
print(i)
输出结果
[[ 4 5 6 7]
[16 17 18 19]]
j = a[0,:,-1]
print(j)
输出结果
[ 3 7 11]
k = a[0,:,1]
print(k)
输出结果
[1 5 9]
l = a[0,::-1,-1]
print(l)
输出结果
[11 7 3]
m = a[0,::2,-1]
print(m)
输出结果
[ 3 11]
n = a[::-1]
print(n)
输出结果
[[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]]
网友评论