General Numerical Python question

Western Larch larix_occidentalis at yahoo.com
Wed Oct 15 22:54:48 EDT 2003


Michael Ressler <ressler at cheetah.jpl.nasa.gov> wrote:

[...]
> from Numeric import *
> a=array([1.,2.,-3.,4.,-5.,6.,-7.,-8.,9.])	# make up an array
> idx=nonzero(a<0)			# indexes of the negative values
> sqrs=sqrt(abs(take(a,idx)))		# get the sqrts of neg elements
> put(a,idx,sqrs)				# put them back into a
> print a					# works!  
> 
> You can make the whole thing a one-liner if you want to get carried
> away with it. It's too bad "nonzero" isn't called "whereis" or
> something like that - it would make the idx= line more obvious.

In the Octave programming language (Matlab-like), the operation
is called "find" -- find(a<0) returns the indices of the negative
elements; a(find(a<0)) returns the negative elements themselves.

With all due respect, Octave rocks pretty hard, and although I
love Python I've only fiddled around with Numeric Python a little
bit -- I haven't found any reason to prefer it to Octave.

FWIW
L.O.




More information about the Python-list mailing list