1-D Array

Indexing

Use bracket notation [ ] to get the value at a specific index. Remember that indexing starts at 0.

 import numpy as np
a=np.arange(12)
a
# start from index 0
a[0]
# the last element
a[-1]

Output:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])

0

11

Slicing

Use : to indicate a range.

array[start:stop] 

A second : can be used to indicate step-size.

array[start:stop:stepsize]

Leaving start or stop empty will default to the beginning/end of the array.

 a[1:4]
a[-4:]
a[-5::-2] #starting 5th element from the end, and counting backwards by 2 until the beginning of the array is reached

Output:

array([1, 2, 3, 4])

array([ 8,  9, 10, 11])

array([7, 5, 3, 1])

Multidimensional Array

 r = np.arange(36)
r.resize((6, 6))
r

Output:

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]])

Use bracket notation to index:

array[row, column] 

and use : to select a range of rows or columns

 r[2, 2]
r[3, 3:6]
r[:2, :-1]#selecting all the rows up to (and not including) row 2, and all the columns up to (and not including) the last column
r[-1, ::2]#selecting the last row, and only every other element

Output:

14

array([21, 22, 23])

array([[ 0,  1,  2,  3,  4],

[ 6,  7,  8,  9, 10]])

array([30, 32, 34])

We can also select nonadjacent elements by

r[[2,3],[4,5]] 

Output:

array([16, 23])

Conditional Indexing

r[r > 30]

Output:

array([31, 32, 33, 34, 35])

Note that if you change some elements in the slice of an array, the original array will also be change. You can see the following example:

 r2 = r[:3,:3]
print(r2)
print(r)
r2[:] = 0
print(r2)
print(r)

Output:

[[ 0  1  2]

[ 6  7  8]

[12 13 14]]

[[ 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]]

[[0 0 0]

[0 0 0]

[0 0 0]]

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

To avoid this, use r.copy to create a copy that will not affect the original array.

 r_copy = r.copy()
print(r_copy, '\n')
r_copy[:] = 10
print(r_copy, '\n')
print(r)

Output:

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

[[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]

[10 10 10 10 10 10]]

[[ 0  0  0  3  4  5]

[ 0  0  0  9 10 11]

[ 0  0  0 15 16 17]

[18 19 20 21 22 23]

[24 25 26 27 28 29]

[30 31 32 33 34 35]]

[Python Cookbook] Numpy Array Slicing and Indexing的更多相关文章

  1. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

  2. [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate

    数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...

  3. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  4. [Python Cookbook] Numpy: Multiple Ways to Create an Array

    Convert from list Apply np.array() method to convert a list to a numpy array: import numpy as np myl ...

  5. python的numpy.array

    为什么要用numpy Python中提供了list容器,可以当作数组使用.但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3].就需要三个指针和三 ...

  6. 【python】numpy array特殊数据统一处理

    array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...

  7. python 中 numpy array 中的维度

    简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...

  8. [Python Cookbook] Numpy: Iterating Over Arrays

    1. Using for-loop Iterate along row axis: import numpy as np x=np.array([[1,2,3],[4,5,6]]) for i in ...

  9. [Python Cookbook] Numpy: How to Apply a Function to 1D Slices along the Given Axis

    Here is a function in Numpy module which could apply a function to 1D slices along the Given Axis. I ...

随机推荐

  1. 笔记-编程-IO模型

    笔记-编程-IO模型 1.      简介 常用IO模型 1)      同步阻塞IO(Blocking IO) 2)      同步非阻塞IO(Non-blocking IO) 3)      IO ...

  2. HDU 3848 CC On The Tree 树形DP

    题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...

  3. linux学习(一) -- ubuntu下lamp环境的配置

    以下为实测教程,希望能为大家提供帮助,转载请注明出处 ubuntu+apache+mysql+php7 第一.更换apt的源 1.复制原文件备份 sudo cp /etc/apt/source.lis ...

  4. 在 Amazon AWS 搭建及部署网站:(三)开发及部署环境

    服务器已经搭建好,网站也开始运行了.那么如何方便地部署代码呢? 最基本的方式,就是使用 SFTP 向网站目录直接部署.这种方法的缺点是版本控制不便,在上传时也无法方便的比较代码变化. 用SVN来部署是 ...

  5. NodeJs初学者经典入门解析

    Node.js 是一个基于谷歌浏览器JavaScript执行环境建立的一个平台,让JavaScript可以脱离客户端浏览器运行,让 JavaScript具有服务器语言的能力.我们可以使用NodeJs方 ...

  6. 44、gridview实现下拉刷新、上拉加载更多(最简单实现上下拉操作的开源工程!)

    1.工程加入以下两个文件夹:(参考:https://github.com/jingchenUSTC/PullToRefreshAndLoad) (待会我会将demo打包上传) 2.这个demo只有一个 ...

  7. Windows系统中 JDK安装及环境配置

    需要安装jdk的第一步就是先去官网下载好JDK,选择需要的版本. Windows系统 1.将下载好的压缩包解压,点击解压得到的jdk执行文件开始安装.在安装过程中会弹出两个安装,一个是jdk,一个是j ...

  8. CentOS7安装Code::Blocks

    在CentOS7上安装Codelocks的过程. 1.安装gcc,需要c和c++两部分,默认安装下,CentOS不安装编译器的,在终端输入以下命令即可yum install gccyum instal ...

  9. [笔记]《算法图解》第十章 K最近邻算法

    K最近邻算法 简称KNN,计算与周边邻居的距离的算法,用于创建分类系统.机器学习等. 算法思路:首先特征化(量化) 然后在象限中选取目标点,然后通过目标点与其n个邻居的比较,得出目标的特征. 余弦相似 ...

  10. [已解决]Argument list too long如何处理?

    Argument list too long 本质是需要处理的长度超过系统的长度,因此无法执行相关命令. 经过搜索发现了两种方法,思想都是将参数切分成小的段落进行执行. 法一:通过xargs传递参数 ...