一维数组的切片访问
numpy 中的一维数组的切片方法与 python 内置的list 切片类似.
Details:
1. ndarray[start:stop:step] # means start from start, stop at stop, step by step
2. ndarray[start:stop] # means start from start, stop at stop, step by 1
3. ndarray[start:] # means start from start, stop at the end, step by 1
4. ndarray[:stop] # means start from the beginning, stop at stop, step by 1
5. ndarray[:] # means start from the beginning, stop at the end, step by 1
6. ndarray[start:stop:step, start:stop:step] # means 2-dimensional slicing
Note:
start is from 0
start is inclusive, stop is exclusive
step could be negative, which means reverse order
下面是例子
先构建1个1维数组:
arr = np.arange(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
logger.info(f"arr: {
arr}") # [0 1 2 3 4 5 6 7 8 9]
- 如果stop index > 数组长度, 则只会返回 start: last index + 1
logger.info(f"arr[1:100]: {
arr[1:100]}") # [1 2 3 4 5 6 7 8 9] even stop > len(arr)
<