NumPy库学习笔记(4) NumPy数组的索引和切片

参考链接: NumPy官网

参考链接: NumPy: the absolute basics for beginners

参考链接: Quickstart tutorial

参考链接: Broadcasting广播

参考链接: NumPy 中文教程

参考链接: Python数据分析与展示

数组的索引和切片

索引:获取数组中特定位置元素的过程
切片:获取数组元素子集的过程

实验结果展示如下:

>>> 
>>> 
>>> a = np.array([9,8,7,6,5])
>>> a[2]
7
>>> a[4]
5
>>> a[1:4:2]
array([8, 6])
>>> a[::-1]
array([5, 6, 7, 8, 9])
>>> a
array([9, 8, 7, 6, 5])
>>> a = np.arange(24).reshape((2,3,4))
>>> a
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]]])
>>> a[1,2,3]
23
>>> a[0,1,2]
6
>>> a[-1,-2,-3]
17
>>> a
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]]])
>>> a[:,1,-3]
array([ 5, 17])
>>> a[:,1:3,:]
array([[[ 4,  5,  6,  7],
        [ 8,  9, 10, 11]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> a[:,:,::2]
array([[[ 0,  2],
        [ 4,  6],
        [ 8, 10]],

       [[12, 14],
        [16, 18],
        [20, 22]]])
>>> 

你可能感兴趣的:(NumPy学习笔记,Python基础实验,python,numpy)