Numeric array equivalent of index ?

N

Nicolas Pernetty

Hello,

I'm trying to find a clear and fast equivalent to the index method of
plain python list :
a = [5,1,4,3,4]
a.index(4)
2

I have to use it on a Numeric array, so the best I've come up with is
(rather ugly I think) :
from Numeric import array, equal, nonzero
a = array([5,1,4,3,4])
nonzero(equal(a,4))[0]
2


Does someone know of a more Pythonic way ?

Thanks,
 
R

Robert Kern

Nicolas said:
Hello,

I'm trying to find a clear and fast equivalent to the index method of
plain python list :
a = [5,1,4,3,4]
a.index(4)

2

I have to use it on a Numeric array, so the best I've come up with is
(rather ugly I think) :
from Numeric import array, equal, nonzero
a = array([5,1,4,3,4])
nonzero(equal(a,4))[0]

2

Does someone know of a more Pythonic way ?

There's a list for Numeric. You'll get better answers there.

http://numeric.scipy.org/

And no, no one has bothered adding a .index() equivalent for general,
unsorted arrays.

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top