Python学习-Numpy-2

1、切片索引

1.1 一维数组的索引

  • 一维数组的索引有点类似list的索引方式,list[1:5],即从第1取到第4个元素,不包含5,左闭右开。
  • list[2:-1],即从第2个元素取到倒数第2个元素 
  • list[2:],即从第2个元素取到倒数第1个元素 
import numpy as np

x=np.arange(10)

print("x=",x)

#基础索引
print("x[1]=",x[1])

x= [0 1 2 3 4 5 6 7 8 9]
x[1]= 1

1.2 二维数组的索引

array[row,column]

此外,numpy切片修改数组中的值会修改原数组

import numpy as np

y=np.arange(12).reshape(3,4)

print("y=",y)

#返回y[1,2]元素,下标从0开始
print("y[1,2]=",y[1,2])

#返回2,3行元素
print("y[1:]=",y[1:])

#返回2,3列元素
print("y[:,1:3]=",y[:,1:3])

y= [[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
y[1,2]= 6
y[1:]= [[ 4  5  6  7]
 [ 8  9 10 11]]
y[:,1:3]= [[ 1  2]
 [ 5  6]
 [ 9 10]]


 2、神奇索引

  •  即下标为列表,根据列表下标找数组中的函数值

2.1 一维索引

import numpy as np

x=np.random.randint(1,10,10)

print("x=",x)

#位置下标列表
index=[3,4,7]

#y代表数组x中下标为3,4,7对应的元素值
y=x[index]
print("y=",y)

x= [8 2 9 8 1 8 2 9 4 9]
y= [8 1 9]

import numpy as np

x=np.random.randint(1,10,10)

print("x=",x)

#位置下标列表
index=np.array([[1,2],[3,4]])

#y代表数组x中下标为3,4,7对应的元素值
y=x[index]
print("y=",y)

x= [2 9 5 2 6 3 6 2 1 5]
y= [[9 5]
 [2 6]]

2.2  二维索引

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

# 筛选多行,列可以省略,即index=[0,2]等价index=[[0,2],:]
index=[0,2]
print("x[index]=",x[index])

 x= [[19 98 34 59]
 [68 54 84 39]
 [55  3 28 37]]
x[index]= [[19 98 34 59]
 [55  3 28 37]]

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

# 筛选多列,行不可以省略
print("x[index]=",x[:,[2,3]])

x= [[58 77 44 34]
 [35 34 80 17]
 [46  8 20  2]]
x[index]= [[44 34]
 [80 17]
 [20  2]]

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

# 同时指定行与列,则返回的一维的数组
print("x[index]=",x[[0,1,2],[2,1,2]])

x= [[97 46 37 32]
 [50 41 50 18]
 [12 41 51 28]]
x[index]= [37 41 51]


3、布尔索引

3.1 一维数组

import numpy as np

x=np.random.randint(1,100,10)
print("x=",x)

#数组中大于5的数为True,小于5的数为False
print(x>5)

 x= [ 3 62 68 80 99 17 87 73 16 55]
[False  True  True  True  True  True  True  True  True  True]

import numpy as np

x=np.random.randint(1,100,10)
print("x=",x)

#输出数组中大于50的数字
print(x[x>50])

 x= [64 92 74 28 36 23 18 46 39 44]
[64 92 74]

import numpy as np

x=np.random.randint(1,100,10)
print("x=",x)

#数组中小于50的数变为0,大于等于50的数变为1
x[x<50]=0
x[x>=50]=1
print(x)

 x= [75 33 28 46 75 55 90 22 43 11]
[1 0 0 0 1 1 1 0 0 0]

 3.2二维数组

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

#数组中大于50的数为True,小于50的数为False
print(x>50)

x= [[40 68 46 14]
 [80 19 90 10]
 [33 86 52 36]]
[[False  True False False]
 [ True False  True False]
 [False  True  True False]]

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

#输出数组中大于50的数
print(x[x>50])

 x= [[28 99 80 56]
 [16 47 66  2]
 [61 12 79 32]]
[99 80 56 66 61 79]

 条件组合查询

import numpy as np

x=np.random.randint(1,100,(3,4))
print("x=",x)

#输出大于50并且为偶数的数
condition=(x>50) & (x%2==0)
print(x[condition])

x= [[69 10 70 78]
 [70 27 15 52]
 [75 89 18  6]]
[70 78 70 52]

  • 28
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值