[Numpy-discussion] Iterate over all 1-dim views

Stefan van der Walt stefan@sun.ac...
Mon Oct 8 05:52:19 CDT 2007


On Sun, Oct 07, 2007 at 06:52:11AM -0400, Neal Becker wrote:
> Suppose I have a function F(), which is defined for 1-dim arguments.  If the
> user passes an n>1 dim array, I want to apply F to each 1-dim view.
> 
> For example, for a 2-d array, apply F to each row and return a 2-d result.
> 
> For a 3-d array, select each 2-d subarray and see above.  Return 3-d result.
> 
> Any suggestions on how to code something like this in numpy?

Not the most efficient way, but easy to read and understand:

import numpy as N

def func(a):
    return a.shape

z = N.zeros((2,2,2,2))
print N.array([func(sub) for sub in z])

Regards
Stéfan


More information about the Numpy-discussion mailing list