Bootstrap

Numpy 数组切片

一、列表切片(一维数组)

1.1、切片原理

列表切片是从原始列表中提取列表的一部分的过程。在列表切片中,我们将根据所需内容(如,从何处开始,结束以及增量进行切片)剪切列表。Python中符合序列的有序序列都支持切片(slice),例如列表,字符串,元组。

规则:

存储对象[start : end : step] 

start : 起始索引,从0开始,-1表示结束
end:结束索引,不包含
step:步长;步长为正时,从左向右取值。步长为负时,反向取值

在这里插入图片描述

1.2、切片使用

1.2.1、获取列表中的元素
>>> l1 = [3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1
[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> midd_num=int(len(l1)/2)
>>> midd_num
5
>>> l1[midd_num:]      # 获取下标 5 及其之后的数据
[15, 17, 20, 23, 25]
>>> l1[:midd_num]      # 获取下标 5 之前的数据
[3, 5, 7, 10, 13]
>>> l1[-1]             # 获取列表最后一个数据
25
>>> l1[-2]             # 获取列表逆序第二个数据
23
>>> l1[-2:]            # 获取列表逆序后两个数据
[23, 25]
>>> l1[2:8]            # 获取列表3-8d的数据
[7, 10, 13, 15, 17, 20]
>>> l1[::2]            # 获取整个列表且步长为2
[3, 7, 13, 17, 23]
>>> l1[1::2]           # 从第二个开始获取整个列表且步长为2
[5, 10, 15, 20, 25]
>>> l1[0:90:2]         # !!!不存在越界问题,体现了健壮性
[3, 7, 13, 17, 23]     
1.2.2、列表逆序

通过设置步长为 -1实现,如下:

>>> l1[::-1]
[25, 23, 20, 17, 15, 13, 10, 7, 5, 3]
1.2.3、修改列表元素

切片赋值的办法实现,如下:

>>> l1
[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[0:1]
[3]
>>> l1[0:1]=["hello"]
>>> l1
['hello', 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[1:2]
[5]
>>> l1[1:2]="world"    # 注意赋值类型需要为列表
>>> l1
['hello', 'w', 'o', 'r', 'l', 'd', 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[0:2]
[3, 5]
>>> l1[0:2]=["hello", "world"]   # 同时修改多个数据
>>> l1
['hello', 'world', 7, 10, 13, 15, 17, 20, 23, 25]
1.2.4、插入新元素

在空白处插入,如下:

>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[:0]=["nihao"]
>>> l1
['nihao', 3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[:1]=["nihao","shijie"]   # 会替换掉 '3'
>>> l1
['nihao', 'shijie', 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[:1]
[3]
>>> l1[:0]=["nihao","shijie"]  # 插入多个
>>> l1
['nihao', 'shijie', 3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[5]
15
>>> l1[5:5]=["nihao", "shijie"]
>>> l1
[3, 5, 7, 10, 13, 'nihao', 'shijie', 15, 17, 20, 23, 25]
1.2.5、删除元素

给列表某个值赋空值,如下:

>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[:3]
[3, 5, 7]
>>> l1[:3]=[]
>>> l1
[10, 13, 15, 17, 20, 23, 25]

>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l1[:3]
[3, 5, 7]
>>> del(l1[:3])    # 同样可以实现上面结果
>>> l1
[10, 13, 15, 17, 20, 23, 25]
1.2.6、复制元素(浅拷贝)
>>> l1=[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l2=l1[:]
>>> l2
[3, 5, 7, 10, 13, 15, 17, 20, 23, 25]
>>> l2 is l1
False
>>> l2=l1
>>> l2 is l1
True
>>> import copy
>>> l2=copy.copy(l1)       # 浅拷贝
>>> l2 is l1
False
>>> l2=copy.deepcopy(l1)   # 深拷贝
>>> l2 is l1
False

二、多维数组切片

多为数组的切片操作与一维数组一致,不同维度上的操作使用’,'隔开就好

2.1、认识数组的维度

arr.ndim

>>> ar1=np.arange(12).reshape((4, 3))
>>> ar1
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> ar1.ndim
2
>>> ar1=np.arange(27).reshape((3,3,3))
>>> ar1
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])

>>> ar1.ndim
3
>>> ar1[:]        # 0 维取全部
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar1[:,0]     # 获取每维数组的第一行
array([[ 0,  1,  2],
       [ 9, 10, 11],
       [18, 19, 20]])
>>> ar1[:,0,0]   # 获取每维数组的第一行的第一个元素
array([ 0,  9, 18])

# 认识数组的维度可以查看ar1.ndim ,也可以查看数组的'['层数

2.2、多维数组切片使用

2.2.1、获取列表中的元素
>>> ar1=np.arange(27).reshape((3,3,3))
>>> ar1
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar1[0]       # 获取数组的0维
array([[0, 1, 2],
       [3, 4, 5],
       [6, 7, 8]])
>>> ar1[1]       # 获取数组的1维
array([[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
>>> ar1[2]       # 获取数组的2维
array([[18, 19, 20],
       [21, 22, 23],
       [24, 25, 26]])
>>> ar1[0,0]
array([0, 1, 2])
>>> ar1[0,0,1]
1
>>> ar1[1,2,1]
16
>>> ar1[1,0,0:2]
array([ 9, 10])
>>> ar1[1,0,-2]
10
>>> ar1[1,0,-2:]
array([10, 11])
2.2.2、数组逆序
>>> ar1=np.arange(27).reshape((3,3,3))
>>> ar1
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar1[1,0,::-1]       # 第2维逆序 
array([11, 10,  9])
>>> ar1[1,::-1]         # 第1 维逆序
array([[15, 16, 17],
       [12, 13, 14],
       [ 9, 10, 11]])
>>> ar1[::-1]           # 整个数组逆序
array([[[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]]])
>>> ar1[::-1,::-1]    # 第0、1维逆序
array([[[24, 25, 26],
        [21, 22, 23],
        [18, 19, 20]],

       [[15, 16, 17],
        [12, 13, 14],
        [ 9, 10, 11]],

       [[ 6,  7,  8],
        [ 3,  4,  5],
        [ 0,  1,  2]]])
>>> ar1[::-1,::-1,::-1]  # 第0、1和2维逆序
array([[[26, 25, 24],
        [23, 22, 21],
        [20, 19, 18]],

       [[17, 16, 15],
        [14, 13, 12],
        [11, 10,  9]],

       [[ 8,  7,  6],
        [ 5,  4,  3],
        [ 2,  1,  0]]])
>>> ar1[1,:,:]
array([[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])
>>> ar1[1,...]     # 对于大于等于三维的数组,可以使用...来简化操作
array([[ 9, 10, 11],
       [12, 13, 14],
       [15, 16, 17]])

2.2.3、修改列表元素
>>> ar1[0,0,1]=999
>>> ar1
array([[[  0, 999,   2],
        [  3,   4,   5],
        [  6,   7,   8]],

       [[  9,  10,  11],
        [ 12,  13,  14],
        [ 15,  16,  17]],

       [[ 18,  19,  20],
        [ 21,  22,  23],
        [ 24,  25,  26]]])
>>> ar1[0,1]
array([3, 4, 5])
>>> ar1[0,1]=[123, 456, 789]
>>> ar1
array([[[  0, 999,   2],
        [123, 456, 789],
        [  6,   7,   8]],

       [[  9,  10,  11],
        [ 12,  13,  14],
        [ 15,  16,  17]],

       [[ 18,  19,  20],
        [ 21,  22,  23],
        [ 24,  25,  26]]])
2.2.4、插入新元素
多维数组空白处插入数据不生效
>>> ar1[0,0,:0]=[58]
>>> ar1[0,0]
array([0, 1, 2])
>>> ar1
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
2.2.5、删除元素

多维数组无法直接删除,报错如下:

>>> ar1[0,1,2]=[]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: setting an array element with a sequence.
2.2.6、复制元素(浅拷贝)
>>> ar1
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar3=ar1[:]
>>> ar3
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar3 is ar1
False
>>> ar3=ar1
>>> ar3 is ar1
True
>>> import copy
>>> ar3=copy.copy(ar1)
>>> ar3
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar3 is ar1
False
>>> ar3=copy.deepcopy(ar1)
>>> ar3
array([[[ 0,  1,  2],
        [ 3,  4,  5],
        [ 6,  7,  8]],

       [[ 9, 10, 11],
        [12, 13, 14],
        [15, 16, 17]],

       [[18, 19, 20],
        [21, 22, 23],
        [24, 25, 26]]])
>>> ar3 is ar1
False

三、参考文档

1、https://blog.csdn.net/hlx20080808/article/details/127610664

2、http://coolpython.net/data_analysis/numpy/numpy-del-item.html

3、https://www.bbsmax.com/A/gAJGw4g1JZ/

4、https://blog.csdn.net/weixin_36670529/article/details/111307004

;