[Numpy-discussion] index method for array objects?

Todd Miller jmiller at stsci.edu
Thu Jul 25 10:15:03 EDT 2002

Aureli Soria Frisch wrote:

> Hi all,
> Has someone implemented a function for arrays that behaves like the 
> index(*) method for lists (it should then consider something like a 
> tolerance parameter).
> I suppose it could be maybe done with array.tolist() and list.index(), 
> but have someone implemented something more elegant/array-based?
> Thanks in advance
> Aureli
> PD: (*) index receive a value as an argument and retunrs the index of 
> the list member equal to this value...

I think the basics of what you're looking for are something like:

def index(a, b, eps):
      return nonzero(abs(a-b) < eps)

which should return all indices at which the absolute value of the 
difference between elements of a and b differ by less than eps.   e.g.:

 >>> import Numeric
 >>> index(Numeric.arange(10,20), 15, 1e-5)


Todd Miller 			jmiller at stsci.edu
STSCI / SSG			(410) 338 4576

More information about the NumPy-Discussion mailing list