[Numpy-discussion] Find index of repeated numbers in array

Sturla Molden sturla at molden.no
Wed Dec 10 11:47:42 EST 2008


On 12/10/2008 5:38 PM, Ross Williamson wrote:
> Hi Everyone
> 
> I think I'm missing something really obvious but what I would like to  
> do is extract the indexes from an array where a number matches - For  
> example
> 
> data = [0,1,2,960,5,6,960,7]
> 
> I would like to know, for example the indices which match 960 - i.e.  
> it would return 3 and 6

 >>> import numpy
 >>> a = numpy.array([0,1,2,960,5,6,960,7])
 >>> a == 960
array([False, False, False,  True, False, False,  True, False], dtype=bool)
 >>> idx, = numpy.where(a == 960)
 >>> idx
array([3, 6])
 >>> idx.tolist()
[3, 6]


Sturla Molden



More information about the NumPy-Discussion mailing list