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. 笔记-urllib-parse

    笔记-urllib-parse 1. 简介模块官方解释This module defines a standard interface to break Uniform Resource Locato ...

  2. leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

    题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height ...

  3. Python+Selenium练习篇之10-刷新当前页面

    本文介绍如何调用webdriver中刷新页面的方法. 相关脚本代码如下: # coding=utf-8import timefrom selenium import webdriver driver ...

  4. [译]tar打包时忽略某些文件夹内容

    使用tar的 --exclude的选项 $ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.t ...

  5. Solr 配置连接数据库

    前面我们将solr安装并创建了core同时也配置可IK分词器,接下来我们通过配置连接Mysql数据库并把数据导入到solr(使用ik分词器). 1.配置managed-schema文件 Request ...

  6. web自动化测试:watir+minitest(三)

    本文,谢绝转载. 整体框架设计: 1.用例的解耦性.一个测试用例一个脚本.而并非minitest中的N个test写在一个文件中 2.单独调试与全量连跑或部分连跑 3.任意变量.参数配置.这点对后期维护 ...

  7. DFA Minimization

    有点晚了我就不调试了..不过说是这么说我还是过了编译的.. #include<bits/stdc++.h> using namespace std; namespace DFA{ cons ...

  8. python常用内建函数

    1.input 读取控制台的输入,输出的是字符串 2.enumerate 遍历数组的时候,能够将index 和item同时返回,返回的每一项包含index,item 3.isinstance(obje ...

  9. jquery 获取 checkbox 的 checked 状态问题

    这个郁闷了,今天写这个功能的时候发现了问题,上网找了好多资料对照,更加纠结... 事实证明一切,自己测试了N遍,发现网上的说法和自己以前的理解都是错的,不知道大家有没发现. 下面来看看网上大多资料的说 ...

  10. 初识laytpl

    laytpl-精致巧妙的JavaScript模板引擎 这两天在做一个mui项目,列表需要循环很多的数据.在公司同事的指引下认识了这个新的模板--laytpl.我只想说,很好用们很巧妙. 废话不多说,直 ...