Numpy 数组的切片操作
实例+解释如下(表格):关键是要明白python中数组的下标体系。一套从左往右,一套从右往左。
1 import numpy as np
2 import sys
3
4 def main():
5 A=np.array([1,2,3,4,5,6,7,8,9,0])
6 B=A.copy()
7 print A
8 print 'same as print A,',A[:]
9 print 'same as print A,',A[::1]
10 print 'reverse A,',A[::-1]
11 print 'print all elements with 2 interval,',A[::2]
12 print 'print elements from the 4th element with 2 interval,',A[3::2]
13 print 'print elements from the last 4th element with 2 interval,',A[-3::2]
14 print 'print elements from the 2nd to 5th element with 2 interval,',A[1:4:2]
15 print 'reverse print all elements with 2 interval,',A[::-2]
16 print 'reverse print elements from the 4th element with 2 interval,',A[3::-2]
17 print 'reverse print elements from the last 4th element with 2 interval,',A[-3::-2]
18 #print A[a:b:c]: a and b determine range of slide. c determine the interval and direction (forth or reverse)
19 print A[:-3:-1]
20 print A[:3:1]
21 print A[:3:-1]
22 C=A[::-1]
23 C[0]=10
24 print 'A is also changed as how does c change,',A
25 B[9]=0
26 print 'A will not be affected when B change,',A
27 print 'array slide from second element to the last one,',A[1:]
28 print 'array slide from last second element to the first one,',A[:-1]
29
30
31
32 if __name__ == "__main__":
33 main()
| A=np.array([1,2,3,4,5,6,7,8,9,0]) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 |
| 下标系一Index(Left to Right) | ||||||||||
| 下标系二Index(Right to Left) | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
| A[a:b:c] | a和b 确定:切片操作的范围(from a to b, exclude b.), C 确定具体切片的pace和方向(例如:1表示Left to Right,pace=1. -1表示Right to Left,pace=1.)。 | |||||||||
| 实例:A[:3:1] | output: 1,2,3. 注:Index 3处对应的数组值4。1表示从左往右,步的大小是1。所以输出1,2,3 | |||||||||
| 实例:A[:-3:-1] | output:0,9 注:Index -3处对应的数组值8。-1表示从右往左,步的大小是1。所以输出0,9 | |||||||||
| 实例:A[:3:-1] | output:0,9,8,7,6,5 注:Index 3处对应的数组值4。-1表示从右往左,步的大小是1。所以输出0,9,8,7,6,5 | |||||||||
| 实例:A[:-3:1] |
output:1,2,3,4,5,6,7 注:Index -3处对应的数组值8。1表示从左往右,步的大小是1。所以输出1,2,3,4,5,6,7 |
|||||||||
| 实例:A[-3::-1] |
output:8,7,6,5,4,3,2,1 注:Index -3处对应的数组值8。-1表示从右往左,步的大小是1。所以输出8,7,6,5,4,3,2,1 |
|||||||||
Numpy 数组的切片操作的更多相关文章
- Numpy数组对象的操作-索引机制、切片和迭代方法
前几篇博文我写了数组创建和数据运算,现在我们就来看一下数组对象的操作方法.使用索引和切片的方法选择元素,还有如何数组的迭代方法. 一.索引机制 1.一维数组 In [1]: a = np.arange ...
- Numpy入门(二):Numpy数组索引切片和运算
在Numpy中建立了数组或者矩阵后,需要访问数组里的成员,改变元素,并对数组进行切分和计算. 索引和切片 Numpy数组的访问模式和python中的list相似,在多维的数组中使用, 进行区分: 在p ...
- Numpy数组的基本运算操作
一.算术运算符 In [3]: a = np.arange(0,5) Out[3]array([0, 1, 2, 3, 4]) In [4]: a+4 Out[4]: array([4, 5, 6, ...
- Numpy入门 - 数组切片操作
本节主要演示数组的切片操作,数组的切片操作有两种形式:更改原数组的切片操作和不更改原数组的切片操作. 一.更改原数组的切片操作 import numpy as np arr = np.array([1 ...
- Numpy 笔记: 多维数组的切片(slicing)和索引(indexing)【转】
目录 切片(slicing)操作 索引(indexing) 操作 最简单的情况 获取多个元素 切片和索引的同异 切片(slicing)操作 Numpy 中多维数组的切片操作与 Python 中 lis ...
- Numpy数组基本操作(数组索引,数组切片以及数组的形状,数组的拼接与分裂)
一:数组的属性 每个数组都有它的属性,可分为:ndim(数组的维度),shape(数组每个维度的大小),size(数组的总大小),dtype(数组数据的类型) 二:数组索引 和python列表一样,N ...
- Python数据分析之numpy数组全解析
1 什么是numpy numpy是一个在Python中做科学计算的基础库,重在数值计算,也是大部分Python科学计算库的基础库,多用于大型.多维数据上执行数值计算. 在NumPy 中,最重要的对象是 ...
- 21、numpy数组模块
一.numpy简介 numpy官方文档:https://docs.scipy.org/doc/numpy/reference/?v=20190307135750 numpy是Python的一种开源的数 ...
- Numpy 数组操作
Numpy 数组操作 Numpy 中包含了一些函数用于处理数组,大概可分为以下几类: 修改数组形状 翻转数组 修改数组维度 连接数组 分割数组 数组元素的添加与删除 修改数组形状 函数 描述 resh ...
随机推荐
- JPA数据懒加载LAZY和实时加载EAGER(转)
原文:https://www.cnblogs.com/MrSi/p/8081811.html 懒加载LAZY和实时加载EAGER的概念,在各种开发语言中都有广泛应用.其目的是实现关联数据的选择性加载, ...
- js限制上传图片类型和大小
<script type="text/javascript"> function checkFile(brandLogo){ var file=brandLogo.va ...
- Tomcat 环境变量配置
1.变量和常量 i 和 0 2.环境变量 cmd >set 查看所有环境变量 %PATH% 系统指定可执行文件的搜索路径,可以是 .exe .bat String path=“C:\WINDOW ...
- iphone“连接到icloud是出错”的可能原因
百度没能解决"连接到icloud是出错",突然发现是因为禁止了"设置"访问WIFI和蜂窝网络(第三张图所示).
- Google Maps 基础
创建一个简单的 Google 地图 现在让我们创建一个简单的 Google 地图. 以下是显示了英国伦敦的 Google 地图: <!DOCTYPE html> <html> ...
- NSDictionary NSMutableDictionary NSSet NSMutableSet
//description只是返回了一个字符串 // [person description]; // //如果想要打印需要NSLog // NSLog(@"%@& ...
- MOD13A1: MODIS/Terra Vegetation Indices 16-Day L3 Global 500 m SIN Grid V006
https://lpdaac.usgs.gov/node/838 Description The MOD13A1 Version 6 product provides a Vegetation Ind ...
- eclipse如何恢复误删文件
刚刚真的要吓死宝宝了,不是说宝宝心里素质差,是因为刚刚误删的文件实在是太重要了,废话不多说了,正题 如何恢复eclipse误删的文件 1,当时被误删了,可立即 Ctrl+z 即可恢复误删文件; 2,时 ...
- The First Android App----Starting Another Activity
To respond to the button's on-click event, open the activity_main.xml layout file and add the androi ...
- java代码中存在的Big Endian 和 Little Endian
Big Endian 和 Little Endian 详解 Java中的Big(Little)-endian问题的一种解决方法 主机序和网络序 很重要很重要 几种ip存放形式 Big-Endian和 ...