[Numpy-discussion] Iterate over an array

Robert Kern robert.kern@gmail....
Fri Aug 28 12:17:55 CDT 2009


On Fri, Aug 28, 2009 at 10:13, davide lasagna<lasagnadavide@gmail.com> wrote:
> Hi all,
> I ve got a 2d array and i want to iterate over its columns in a "pythonic
> way".
> This is what i have in mind: please consider this snippet:
>
> #################################################
> import numpy as np
>
> array = np.random.standard_normal( (10,10) )
>
> for column in array.some_column_method():
>       column  = do_something()
>
> #################################################
>
> The trivial way do the for loop is:
>
> #################################################
> for i in range(  array.shape[1] ):
>       array[:, i] = do_something()
> #################################################
>
> Is there any way to do what i think?? Can i obtain "pythonically" a list of
> column arrays??

for column in array.transpose():
    column[:] = do_something()

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list