# python散点图图例 - python画图标题

## 用nditer浅迭代 (2)

``````for i in y.reshape(-1,3):
print i``````

``````for x in y:
for z in x:
print z``````

Plain `nditer`迭代`y`每个元素（ `nditer`不会给你索引）：

``````for i in np.nditer(y):
print i
# wrong y[i]``````

``````for x,z in np.nditer([y,y]):
print x,z``````

http://docs.scipy.org/doc/numpy/reference/arrays.nditer.html有关于使用`nditer`的更多信息

``````y3=y.swapaxes(2,0).copy(order='C')
for i in np.nditer(y3,order='F',flags=['external_loop']):
print i,

[242  14 211] [198   7   0] [235  60  81] [164  64 236]``````

``````for i in np.ndindex(y.shape[:2]):
print y[i],
# [242  14 211] [198   7   0] [235  60  81] [164  64 236]``````

`ndindex`使用`nditer` 。 生成浅迭代的技巧是使用一个只使用您想要迭代的维度的子数组。

``````class ndindex(object):
def __init__(self, *shape):
...
x = as_strided(_nx.zeros(1), shape=shape, strides=_nx.zeros_like(shape))
self._it = _nx.nditer(x, flags=['multi_index', 'zerosize_ok'], order='C')
def __next__(self):
next(self._it)
return self._it.multi_index``````

``````xx = np.zeros(y.shape[:2])
it = np.nditer(xx,flags=['multi_index'])
while not it.finished:
print y[it.multi_index],
it.iternext()
# [242  14 211] [198   7   0] [235  60  81] [164  64 236]``````

``````>>>y = np.random.randint(0, 255, (2,2,3))
>>>array([[[242, 14, 211],
[198,  7,   0]],

[[235,  60,  81],
[164,  64, 236]]])``````

``````for i, j in np.nditer(y):
print y[i, j],``````

`[242, 14, 211], [198, 7, 0], [235, 60, 81], [164, 64, 236]` ，但没有运气！

``````Traceback (most recent call last):

File "<ipython-input-21-a336ef837a8a>", line 1, in <module>
for i, j in np.nditer(y):    print y[i,j]

TypeError: iteration over a 0-d array``````

``````from itertools import chain

y = np.random.randint(0, 255, (2,2,3)
b = chain.from_iterable(y) # where b is a generator``````

``````[array([ 51, 119,  84]),
array([ 50, 110, 193]),
array([165, 157,  52]),
array([239, 119,  83])]``````