1. Using for-loop

Iterate along row axis:

 import numpy as np
x=np.array([[1,2,3],[4,5,6]])
for i in x:
print(x)

Output:

[1 2 3]

[4 5 6]

Iterate by index:

for i in range(len(x)):
print(x[i])

Output:

[1 2 3]

[4 5 6]

Iterate by row and index:

for i, row in enumerate(x):
print('row', i, 'is', row)

Output:

row 0 is [1 2 3]

row 1 is [4 5 6]

 

2. Using ndenumerate object

for index, i in np.ndenumerate(x): 
print(index,i)

Output:

(0, 0) 1

(0, 1) 2

(0, 2) 3

(1, 0) 4

(1, 1) 5

(1, 2) 6

3. Using nditer object

See: https://docs.scipy.org/doc/numpy-1.15.0/reference/arrays.nditer.html

4. Use zip to iterate over multiple iterables

 x2 = x**2
print(x2,'\n')
for i, j in zip(x, x2):
print(i,'+',j,'=',i+j)

Output:

[[ 1  4  9]

[16 25 36]]

[1 2 3] + [1 4 9] = [ 2  6 12]

[4 5 6] + [16 25 36] = [20 30 42]

[Python Cookbook] Numpy: Iterating Over Arrays的更多相关文章

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

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

  2. [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 ...

  3. [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 ...

  4. [Python Cookbook] Numpy Array Slicing and Indexing

    1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...

  5. [Python Cookbook] Numpy Array Manipulation

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

  6. numpy 数组迭代Iterating over arrays

    在numpy 1.6中引入的迭代器对象nditer提供了许多灵活的方式来以系统的方式访问一个或多个数组的所有元素. 1 单数组迭代 该部分位于numpy-ref-1.14.5第1.15 部分Singl ...

  7. [转]python与numpy基础

    来源于:https://github.com/HanXiaoyang/python-and-numpy-tutorial/blob/master/python-numpy-tutorial.ipynb ...

  8. Python之Numpy详细教程

    NumPy - 简介 NumPy 是一个 Python 包. 它代表 “Numeric Python”. 它是一个由多维数组对象和用于处理数组的例程集合组成的库. Numeric,即 NumPy 的前 ...

  9. 【python cookbook】找出序列中出现次数最多的元素

    问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...

随机推荐

  1. hadoop启动时权限不足

    之前在使用时的没用去懂.ssh,后来因为一些情况直接将其权限修改为777. 第一位7等于4+2+1,所以就是rwx,所有者有读取.写入.执行的权限:第二位7也是4+2+1,rwx,同组用户具有读取.写 ...

  2. Linux文件类型 扩展名的作用

    链接类型文件 查找显示管道文件 普通文件类型 file 查看文件的类型 data文件类型 创建块字和符设备 mknod 1,.tar .tar.gz .tgz .zip tar.bz 表示压缩文件,创 ...

  3. 4 Template层 -模板继承

    1.模板继承 模板继承可以减少页面内容的重复定义,实现页面内容的重用 典型应用:网站的头部.尾部是一样的,这些内容可以定义在父模板中,子模板不需要重复定义 block标签:在父模板中预留区域,在子模板 ...

  4. 【NopCommerce 3.1】asp.net mvc 利用jQuery from.js上传用户头像

    纯代码不解释. 在CusotmerControllers中添加上传方法 /// <summary> /// ajax上传用户头像 /// </summary> /// < ...

  5. Django补充知识点——用户管理

    内容概要 1.Form表单2.Ajax3.布局,Django母板4.序列化5.Ajax相关6.分页7.XSS攻击8.CSRF9.CBV.FBV 10.类中用装饰器的两种方法 11.上传文件 12.数据 ...

  6. Leetcode 632.最小区间

    最小区间 你有 k 个升序排列的整数数组.找到一个最小区间,使得 k 个列表中的每个列表至少有一个数包含在其中. 我们定义如果 b-a < d-c 或者在 b-a == d-c 时 a < ...

  7. 微信小程序--问题汇总及详解之picker 增、删

    <block wx:for="{{salesList}}" wx:for-index="index" wx:key="id" wx:f ...

  8. iOS voip视频调试结果

    iOS idoubs + linphone(Windows版本):OK iOS idoubs + X-Lite(Mac OS X版本):OK linphone(或X-Lite)主动发起voice请求, ...

  9. 【bzoj2242】[SDOI2011]计算器 EXgcd+BSGS

    题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...

  10. java.net.BindException: Permission denied

    端口号报错: 解决办法:把端口号改为1000以上的,比如8080