[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

  

猜你喜欢

转载自www.cnblogs.com/sherrydatascience/p/10206788.html