[SciPy-user] Cycling the arrays

Sturla Molden sturla@molden...
Fri Mar 6 11:45:42 CST 2009


> just a quick question: what is the fastest way to cycle inside an N
> dimensional array?
> I just want to visit each and every element in the N dimensional
> array, then do things.
>
> I can do it with N 'for' cycles, obviously.
> Is there a more compact and maybe faster way to do it?

It depends on what you want to do. You have to be more specific than this.

Generally, 'for' loops may be evil but not always. An example of a for
loop that may not be evil is:

    for i in range(20):
        do_a_lot_of_work( array[i,:,:] )

Evil for loops are those that are (1a) very long or (1b) executed
repetitively, and (2) each iteration do a relatively small amount of work.
These are candidates for vectorization or migration to a lower level
language (Cython, C or Fortran).


Sturla Molden



More information about the SciPy-user mailing list