[Python Cookbook] Numpy: Iterating Over Arrays

Posted Sherrrry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python Cookbook] Numpy: Iterating Over Arrays相关的知识,希望对你有一定的参考价值。

1. Using for-loop

Iterate along row axis:

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

Output:

[1 2 3]

[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

  


以上是关于[Python Cookbook] Numpy: Iterating Over Arrays的主要内容,如果未能解决你的问题,请参考以下文章

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

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

Python Cookbook(第3版)中文版:15.11 用Cython写高性能的数组操作

从 C 代码调用 numpy 函数

python图像处理库 哪个好 知乎

OpenCV Cookbook by Eric