当前位置:网站首页>numpy 索引及切片
numpy 索引及切片
2022-06-26 04:28:00 【莉莉妖_】
基本索引及切片
一维数组
import numpy
a = numpy.arange(0,20)
print(a)
print(a[4])
print(a[4:10])
二维数组
import numpy
b = numpy.arange(0,16).reshape(4,4)
print(b)
print(b[2]) # 切片为下一维度的一个元素,所以是一维数组
print(b[2][1]) # 二次索引,得到一维数组中的一个值
print(b[2,1]) # 切片数组中的第3行第2列,与二次索引类似 → 9
print(b[1:3]) # 切片为两个一维数组组成的二维数组
print(b[0:2,1:]) # 切片数组中的1,2行、2,3,4列 → 二维数组
三维数组
import numpy
c = numpy.arange(0,12).reshape(3,2,2) # 3个3行2列数组
print(c)
print(c[2]) # 三维数组的下一个维度的第三个元素 → 一个二维数组
# 三维数组的下一个维度的第三个元素下的第一个元素 → 一个一维数组
print(c[2][0])
# 三维数组的下一个维度的第三个元素下的第一个元素下的第二个元素 → 一个值
print(c[2][0][1])
布尔型索引及切片
ar[i,:] = ar[i](简单书写格式)
使用布尔型的矩阵去做筛选
import numpy
a = numpy.arange(0,12).reshape(3,4)
print(a)
b = numpy.array([True,False,True])
c = numpy.array([True,False,True,False])
print(a[b,:]) # 保留第1,3行
print(a[:,c]) # 保留第1,3列
print(a > 5) # 获得布尔型数组
print(a[a > 5]) # 获得符合条件的值
数组索引及切片值的更改、复制
import numpy
# 一个标量赋值给一个索引/切片时,会自动改变/传播原始数组
a = numpy.arange(0,10)
print(a)
a[5] = 100
a[7:9] = 200
print(a)
# 复制
b = a.copy()
b[7:9] = 300
print(a) # a数组不受影响
print(b) # b数组的数值受到更改
边栏推荐
- List of provinces, cities and counties in China
- [Qunhui] command line acme SH automatically apply for domain name certificate
- MySQL enable logbin in Qunhui docker
- Mobile terminal pull-down loading pull-down loading data
- Guide de la pompe de données Oracle
- 1064 (42000) error occurred when installing MySQL and modifying root password
- [Qunhui] Internet access + custom port
- What are the advantages and risks of paper gold investment
- 一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
- Wechat applet is bound to a dynamic array to implement a custom radio box (after clicking the button, disable the button and enable other buttons)
猜你喜欢
2021/11/6-burpsuit packet capturing and web page source code modification
A brain map to summarize the needs analysis (a supplement to the actual situation at work)
Using jsup to extract images from interfaces
修改Oracle连接数
Spark - understand parquet
win10 系统打开的软件太小,如何变大(亲测有效)
Construction of art NFT trading platform | NFT mall
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)
Sixtool- source code of multi-functional and all in one generation hanging assistant
随机推荐
Computer network high frequency interview questions
College C language final exam · multiple choice questions · summary notes of mistakes and difficulties
Modify the number of Oracle connections
CDN with OSS acceleration
NPM installation tutorial
Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)
WPF value conversion
PHP inherited in class return does not work
SQL related knowledge - DQL
Redis cache message queue
[Nuggets' operation routine disclosure] the routine of being truly Nuggets
SixTool-多功能多合一代挂助手源码
PIP batch complete uninstall package
Clean up photo SCR virus / iframekill injection removal /iframekill removal photo scr
[Qunhui] no port access (reverse proxy + intranet penetration)
Zeromq from getting started to mastering
The select option in laravel admin contains a large amount of data
MySQL index details
Zhubo Huangyu: you can try these non-agricultural operation skills
Analysis report on development trend and market demand of global and Chinese molecular diagnostics industry from 2022 to 2028