[Numpy-discussion] Find first occurrence?

Zachary Pincus zpincus@stanford....
Wed Sep 19 19:37:43 CDT 2007

Hello all,

On several occasions, I've had the need to find only the first  
occurrence of a value in an unsorted numpy array. I usually use  
numpy.where(arr==val)[0] or similar, and don't worry about the fact  
that I'm iterating across the entire array.

However, sometimes the arrays are pretty big and the find operation  
is in an inner loop, so I was wondering if there's already a C  
extension function somewhere in numpy or scipy that does a fast find  
first operation, or anything similar. (Easy enough to write my own,  
and maybe given the issues inherent in comparing float equality,  
etc., it doesn't belong in the core numpy anyway...)


Zach Pincus
Program in Biomedical Informatics and Department of Biochemistry

More information about the Numpy-discussion mailing list