Find index of repeated numbers in array
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 I could do this with a loop but I was wondering if there was a built in numpy function to do this? BTW if anyone is interested I'm converting some idl code to numpy and trying to mmic the IDL function where Cheers Ross
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 as np In[14]: np.where( np.array( data ) == 960 ) Out[14]: (array([3, 6]),) If you need to count all of the items, try something like np.histogram( data, np.max( data ) ) cheers, r.
On Wed, 10 Dec 2008 11:38:11 -0500 Ross Williamson <rw247@astro.columbia.edu> 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
I could do this with a loop but I was wondering if there was a built in numpy function to do this?
BTW if anyone is interested I'm converting some idl code to numpy and trying to mmic the IDL function where
Cheers
Ross _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org
data array([ 0, 1, 2, 960, 5, 6, 960, 7]) where(data==960) (array([3, 6]),)
Nils
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
Thanks all I was being dumb and forgot to initialize as array() Cheers Ross On Dec 10, 2008, at 11:47 AM, Sturla Molden wrote:
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 _______________________________________________ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Nils Wagner -
Robert Cimrman -
Ross Williamson -
Sturla Molden