Numeric slicing, iteration prob.

Fernando Perez fperez528 at
Mon Aug 5 18:20:01 EDT 2002

Duncan Smith wrote:

> Hello,
> Does anyone know of a cuter way of iterating over an index rather
> than building strings and using eval()? (Example below.)  This will get more
> awkward when I need to do further slicing.  eg. depending on the results
> returned I might need to generate t[1,:,3], t[:,1,3] etc.

Can't think of a nicer one right now, but there probably is one. Maybe build 
the necessary index structures and use take()? Don't know...

> On a related note, does anyone have a feeling for the most efficient way of
> checking if all the values in a returned slice are zero?  I can obviously
> just iterate over the cells and break when I get a non-zero value, but I'm
> hoping there's some more efficient method available in Numeric.  TIA.

Look into alltrue:

In [14]: import Numeric as N

In [15]: N.alltrue ?
Type:           function
Base Class:     <type 'function'>
String Form:    <function alltrue at 0x82e31f4>
Namespace:      Currently not defined in user session.
Definition:     N.alltrue(x, axis=0)
    Perform a logical_and over the given axis.

That may help.



More information about the Python-list mailing list