get array element

Robert Kern robert.kern at gmail.com
Thu Apr 10 17:43:25 EDT 2008


Bryan.Fodness at gmail.com wrote:
> I have an array, and I would like to get the indice value.
> 
> a = array([13,14,15,16])
> 
> I would like something like a.getindice(15)
> 
> If I want 15 it would return 2

You will want to ask numpy questions on the numpy mailing list. If you don't 
mention that you are using numpy here, people get confused.

   http://www.scipy.org/Mailing_Lists

Anyways, if your array is sorted, use a.searchsorted(15). If it isn't sorted, 
then you can find all of the indices equal to the value with the following:

   In [7]: from numpy import *

   In [8]: a = array([13,14,15,16])

   In [9]: nonzero(a == 15)[0]
   Out[9]: array([2])

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list