[Numpy-discussion] Multiarray PEP: iterators

Fernando.Perez at colorado.edu Fernando.Perez at colorado.edu
Fri Feb 18 10:03:13 EST 2005

Quoting konrad.hinsen at laposte.net:

> At the moment, iteration over a rank-N array yields rank-(N-1) arrays
> (indexing along the first dimension). I consider this vastly more
> useful than iterating over the rank-0 elements and thus ignoring the
> array structure completely.

Completely agreed.  I'm sure I'm not the only one who has code like

for row in matrix:

Furthermore, this is consistent with python's behavior for nested lists:

In [3]: nested=[['first row',1,2],['second row',3,4]]

In [4]: for row in nested:
   ...:     print row
['first row', 1, 2]
['second row', 3, 4]

Python does not flatten nested structures when iterating over them, neither
should numerix.  I think the addition (which Travis seems keen on) the .flat
iterator attribute is the right approach here.  Normal iterators would maintain
structure (and behave like pyhon's for nested lists), while .flat would return
an element-wise iterator for cases where such a thing is needed (which do
exist, obviously).



More information about the NumPy-Discussion mailing list