By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,077 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

get array element

P: n/a
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
Apr 10 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Apr 10, 9:22 pm, "Bryan.Fodn...@gmail.com"
<Bryan.Fodn...@gmail.comwrote:
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
>>a = array('i', [13,14,15,16])
a.index(15)
2
Apr 10 '08 #2

P: n/a
On Apr 10, 10:22 am, "Bryan.Fodn...@gmail.com"
<Bryan.Fodn...@gmail.comwrote:
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
a.index(15)
Apr 10 '08 #3

P: n/a
Br***********@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

Apr 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.