[Numpy-discussion] very simple iteration question.

Christopher Barker Chris.Barker@noaa....
Wed Apr 30 11:48:01 CDT 2008


a g wrote:
> OK: how do i iterate over an axis other than 0?

This ties in nicely with some of the discussion about interating over 
matrices. It ahs been suggested that it would be nice to have iterators 
for matrices, so you could do:

for row in M.rows:
   ...

and

for column in M.cols:
   ...


If so, then wouldn't it make sense to have built in iterators for 
nd-arrays as well? something like:

for subarray in A.iter(axis=i):
    ...

where axis would default to 0.

This is certainly cleaner than:

for j in range(A.shape[i]):
     subarray = A[:,:,j,:]

Wait! I have no idea how to spell that generically -- i.e. the ith 
index, where i is a variable. There must be a way to build a slice 
object dynamically, but I don't know it.

How often would i be a variable, rather than hard coded? I have no idea.

-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@noaa.gov


More information about the Numpy-discussion mailing list