python二维数组切片规则_python中数组切片含义解析
关于数组切片的解析:
① [i]、[-1];
② [:-1]、[:n]、[n:]、[n:m]、[:];
③ [n: m: x]、[: : x]、[: : ]、[-1: : ]、[: -1:]、[: :-1]
④ [a,b]、[a1:a2, b1:b2]
一、一维数组
1.1 无冒号 —— 结果为一维数组的元素
(1) A[i]:取数组中下标为i的元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[3])
---------------------------------
3
Process finished with exit code 0
(2) A[-1]:取数组中最后一个元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[-1])
---------------------------------
6
Process finished with exit code 0
1.2 一个冒号( : ) —— 结果为一维数组的切片
(1)A[:n]:取从首项至下标为n-1的元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[:4])
---------------------------------
[0, 1, 2, 3]
Process finished with exit code 0
(2) A[:-1]:取数组中除最后一个元素外的所有元素
(-1在此相当于len(A)-1,即从首项至下标为 (len(A)-1)-1 的元素)
A = [0, 1, 2, 3, 4, 5, 6]
print(A[:-1])
---------------------------------
[0, 1, 2, 3, 4, 5]
Process finished with exit code 0
(3)A[n:]:取下标为n的元素至最后一项元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[2:])
---------------------------------
[2, 3, 4, 5, 6]
Process finished with exit code 0
(4)A[-1:]:取数组中最后一个元素
(-1在此相当于len(A)-1,即从下标为 len(A)-1 的元素至最后一项元素,实际上就是最后一项元素)
A = [0, 1, 2, 3, 4, 5, 6]
print(A[-1:])
-------------------------
[6]
Process finished with exit code 0
(5)A[n:m]:取下标为n至下标为m-1的元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[2:5])
---------------------------------
[2, 3, 4]
Process finished with exit code 0
(6)A[:]:取所有元素
A = [0, 1, 2, 3, 4, 5, 6]
print(A[:])
---------------------------------
[0, 1, 2, 3, 4, 5, 6]
Process finished with exit code 0
1.3 两个冒号( : : ) —— 结果为一维数组的切片
(1) A[n: m: x]
n表示元素下标为n,缺省时默认值为0(即第1项);
A = [0, 1, 2, 3, 4, 5, 6]
print(A[:2:1])
print(A[0:2:1])
---------------------------------
[0, 1]
[0, 1]
Process finished with exit code 0
m表示元素下标为m-1,缺省时默认值为len(A)(即最后一项);
A = [0, 1, 2, 3, 4, 5, 6]
print(A[0::1])
print(A[0:7:1])
print(A[0:5:1])
---------------------------------
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4]
Process finished with exit code 0
x表示步长,缺省时默认值为1;
A = [0, 1, 2, 3, 4, 5, 6]
print(A[0:7:]) #缺省时默认为1
print(A[0:7:1]) #步长为1
print(A[0:7:2]) #步长为2
---------------------------------
[0, 1, 2, 3, 4, 5, 6]
[0, 1, 2, 3, 4, 5, 6]
[0, 2, 4, 6]
Process finished with exit code 0
(2) A[: : x]
n和m都缺省时,范围为整个数组,元素的选择取决于步长
x为-1时,取步长为1的倒序
A = [0, 1, 2, 3, 4, 5, 6]
print(A[::1])
print(A[::2])
print(A[::-1])
---------------------------------
[0, 1, 2, 3, 4, 5, 6]
[0, 2, 4, 6]
[6, 5, 4, 3, 2, 1, 0]
Process finished with exit code 0
(3) A[: : ]
A = [0, 1, 2, 3, 4, 5, 6]
print(A[::])
---------------------------------
[0, 1, 2, 3, 4, 5, 6]
Process finished with exit code 0
(4) A[-1: : ]、A[: -1:]、A[::-1]
A = [0, 1, 2, 3, 4, 5, 6]
print(A[-1::])
print(A[:-1:])
print(A[::-1])
---------------------------------
[6]
[0, 1, 2, 3, 4, 5]
[6, 5, 4, 3, 2, 1, 0]
Process finished with exit code 0
二、二维数组
2.1 A[a, b] —— 结果为二维数组中的元素
a表示取第一维的下标为a的元素,b表示取第二维的下标为b的元素
import numpy as np
A = np.array([[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]])
print(A[2, 3])
# 先取第一维的下标为2的元素,即[20, 21, 22, 23];
# 再取第二维的下标为3的元素,即23.
---------------------------------
23
Process finished with exit code 0
2.2 A[a1:a2, b1:b2] —— 结果为二维数组的切片
a1:a2表示取第一维的下标为a1至a2-1的数组切片;
b1:b2表示取第二维的下标为b1至b2-1的数组切片。
import numpy as np
A = np.array([[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]])
print(A[1:2, 3:4])
# 先取第一维的下标为1至(2-1)的元素,即[[10, 11, 12, 13]];
# 再取第二维的下标为3至(4-1)的元素,即[[13]].
print(A[]
---------------------------------
[[13]]
Process finished with exit code 0
import numpy as np
A = np.array([[0, 1, 2, 3, 4], [10, 11, 12, 13, 14], [20, 21, 22, 23, 24]])
print(A[1:3, 3:5])
# 先取第一维的下标为1至(3-1)的元素,即[[10, 11, 12, 13, 14],[20, 21, 22, 23, 24]];
# 再取第二维的下标为3至(5-1)的元素,即[[13 14] [23 24]].
---------------------------------
[[13 14]
[23 24]]
Process finished with exit code 0
当A[a1:a2, b1:b2]中有数值缺省时,规则同一维数组。
【python二维数组切片规则_python中数组切片含义解析】此外,更高维数组的切片以此类推。
推荐阅读
- python|使用Python进行压缩与解压缩
- python梯度下降法实现线性回归_梯度下降法的python代码实现(多元线性回归)
- 基于python的数据爬取与分析_基于Python的网站数据爬取与分析的技术实现策略
- python|5个必知必会的 Python 数据合并技巧
- python|python 三维数组 txt_python(其他语言也可以)如何保存和拼接三维数组()
- OpeenCV(python)|openCV专栏(一)(基础操作)
- python第三方库有哪些-Python常用第三方库大盘点
- Quant|量化分析师的Python日记【Q Quant兵器谱之函数插值】
- OpenCV|opencv3/python 鼠标响应操作
- [Golang]力扣Leetcode - 53.最大子数组和(动态规划)