[Numpy-discussion] match or vectorized in-type function.

James Bullard jbullard@pacificbiosciences....
Mon Jul 12 21:12:21 CDT 2010


I have two vectors of integers of not necessarily the same length.
Consider the hypothetical function match (or if you are familiar to R
then consider that function). 

match(v1, v2) => returns a boolean array of length len(v1) indicating
whether element i in v1 is in v2. 

I cannot find this function in numpy. I would assume a variant is there,
but not being able to find it I wrote it myself. First, is there such a
function? Second, if there is not, is this implementation reasonable?

thanks, jim


from numpy import *

a2 = random.randint(1, 1000, 1000)
a1 = random.randint(1, 10000000, 100000)

def match(v1, v2):
    d = dict(zip(v2, range(0, len(v2))))
    return array([ d.has_key(x) for x in v1])


More information about the NumPy-Discussion mailing list