|
array([:])
>>> import numpy as np
>>>
>>> x=np.array([1,2,3,4,5,6,7,8,9,10,11,12])
>>> print(x[1:5])#打印index为1~5的数组,范围是左闭右开
[2 3 4 5]
>>> print(x[3:])#打印index=3之后的数组,包含index=3
[ 4 5 6 7 8 9 10 11 12]
>>> print(x[:9])#打印index=9之前的数组,不包含index=9
[1 2 3 4 5 6 7 8 9]
>>> print(x[1:-2])#打印index=1到倒数第2个index之间的数组
[ 2 3 4 5 6 7 8 9 10]
>>> print(x[-9:-2])#打印倒数第9个index和倒数第2个index之间的数组,左开右闭
[ 4 5 6 7 8 9 10]
array([::])
>>> print(x[1::3])#以index=1为起始位置,间隔3
[ 2 5 8 11]
>>> print(x[::3])#默认从index=0开始,间隔3
[ 1 4 7 10]
>>> print(x[3::])#和[3:]一样
[ 4 5 6 7 8 9 10 11 12]
>>> print(x[::-1])#反向打印数据,从最后一个index开始,间隔为1
[12 11 10 9 8 7 6 5 4 3 2 1]
>>> print(x[::-3])#反向打印数据,从最后一个index开始,间隔为3
[12 9 6 3]
>>> print(x[7:2:-1])#反向打印index=2(不包含)到index=7之间的数据
[8 7 6 5 4]
|
|