维小维写作训练营python学习笔记

python:numpy的索引和切片(1)

2019-08-13  本文已影响0人  书生_Scholar

首先导入numpy

In [1]:mport numpy as np
In [2]:t = np.arange(80).reshape(20,4)
In [3]: t
Out[3]: 
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, 27],
       [28, 29, 30, 31],
       [32, 33, 34, 35],
       [36, 37, 38, 39],
       [40, 41, 42, 43],
       [44, 45, 46, 47],
       [48, 49, 50, 51],
       [52, 53, 54, 55],
       [56, 57, 58, 59],
       [60, 61, 62, 63],
       [64, 65, 66, 67],
       [68, 69, 70, 71],
       [72, 73, 74, 75],
       [76, 77, 78, 79]])
In [4]:t[2]                            # 取第二行
Out[4]: array([ 8,  9, 10, 11])

-- 2)取多行—取连续多行

In [5]:t[2:]
array([[ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19],
       [20, 21, 22, 23],
       [24, 25, 26, 27],
       [28, 29, 30, 31],
       [32, 33, 34, 35],
       [36, 37, 38, 39],
       [40, 41, 42, 43],
       [44, 45, 46, 47],
       [48, 49, 50, 51],
       [52, 53, 54, 55],
       [56, 57, 58, 59],
       [60, 61, 62, 63],
       [64, 65, 66, 67],
       [68, 69, 70, 71],
       [72, 73, 74, 75],
       [76, 77, 78, 79]])

-- 3)取多行—取不连续多行

In [6]:t[[2,5,8,12,16]]        # 取不连续多行,一定是方括号套方括号才符合语法规则
Out[6]: 
array([[ 8,  9, 10, 11],
       [20, 21, 22, 23],
       [32, 33, 34, 35],
       [48, 49, 50, 51],
       [64, 65, 66, 67]])
In [7]:t[:2,:]                #t[,],","前是行操作,","之后是列操作
Out[7]: 
array([[0, 1, 2, 3],       # 取前3行 和所有列    
       [4, 5, 6, 7]])
In [8]: t[[2,5,8,12,16],:]     # 取2、5、8、12、16行,以及取全部列数据
Out[8]: 
array([[ 8,  9, 10, 11],         # 请比较以上多行操作
       [20, 21, 22, 23],
       [32, 33, 34, 35],
       [48, 49, 50, 51],
       [64, 65, 66, 67]])
In [11]:t[:,2]                  # 取一列(第3列)数据
Out[11]: array([ 2,  6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 54, 58, 62, 66,
       70, 74, 78])
In [12]:t[:,:2]                # 取连续多列 
Out[12]: 
array([[ 0,  1],
       [ 4,  5],
       [ 8,  9],
       [12, 13],
       [16, 17],
       [20, 21],
       [24, 25],
       [28, 29],
       [32, 33],
       [36, 37],
       [40, 41],
       [44, 45],
       [48, 49],
       [52, 53],
       [56, 57],
       [60, 61],
       [64, 65],
       [68, 69],
       [72, 73],
       [76, 77]])
In [13]:t[:,[0,2]]     # 取不连续多列(第0列和第2列)
Out[13]: 
array([[ 0,  2],
       [ 4,  6],
       [ 8, 10],
       [12, 14],
       [16, 18],
       [20, 22],
       [24, 26],
       [28, 30],
       [32, 34],
       [36, 38],
       [40, 42],
       [44, 46],
       [48, 50],
       [52, 54],
       [56, 58],
       [60, 62],
       [64, 66],
       [68, 70],
       [72, 74],
       [76, 78]])
In [21]:t[6,2]              # 1 取6行2列
Out[21]: 26
In [22]:t[3:6,1:3]          # 2 取第4行到第6行,第2列到3列
Out[22]: 
array([[13, 14],           
       [17, 18],
       [21, 22]])
In [30]:t[[3,6,13],[0,2,3]]# 3 这种取值方式,只能取到某个或某些点,列和行数要对应,否则要报错☆☆☆☆☆☆
Out[30]: array([12, 26, 55])
上一篇下一篇

猜你喜欢

热点阅读