[Numpy-discussion] very simple iteration question.

Christopher Barker Chris.Barker at noaa.gov
Wed Apr 30 14:57:44 EDT 2008


Anne Archibald wrote:
>> it's much easier to just write
> 
> for subarray in np.rollaxis(A,i):
>     ...

cool, thanks! So the answer to the OPs question:

> OK: how do i iterate over an axis other than 0?
> 
> I have a 3D array of data[year, week, location].  I want to iterate
> over each year at each location  ...

for loc in np.rollaxis(data, 2):
     for year in np.rollaxis(data, 0): # rollaxis not required here, but
                                       # for symmetry's sake...

     ....


I think I still like the idea of an iterator (or maybe making rollaxis a 
method?), but this works pretty well.

-Chris



-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov



More information about the NumPy-Discussion mailing list