2017-11-25T05:08:15Z||2017-11-25T05:08:15Z
- for会把二维数组当做一维数组,直接打印出两个row。
np.ndenumerate
会把所有元素的index和value枚举出来。np.ndindex(a.shape)
只会把index枚举出来。
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print('for x in a:')
for x in a:
print(x)
print('for x in np.ndenumerate(a):')
for x in np.ndenumerate(a):
print(x)
print('for (x, y), value in np.ndenumerate(a):')
for (x, y), value in np.ndenumerate(a):
print((x, y), value)
print('for (x, y) in np.ndindex(a.shape):')
for (x, y) in np.ndindex(a.shape):
print((x, y))
打印:
for x in a:
[1 2 3]
[4 5 6]
for x in np.ndenumerate(a):
((0, 0), 1)
((0, 1), 2)
((0, 2), 3)
((1, 0), 4)
((1, 1), 5)
((1, 2), 6)
for (x, y), value in np.ndenumerate(a):
((0, 0), 1)
((0, 1), 2)
((0, 2), 3)
((1, 0), 4)
((1, 1), 5)
((1, 2), 6)
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)