match or vectorized in-type function.

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])

match(v1, v2) => returns a boolean array of length len(v1) indicating whether element i in v1 is in v2.
You want numpy.in1d (and friends, probably, like numpy.unique and the others that are all collected in numpy.lib.arraysetops...) Definition: numpy.in1d(ar1, ar2, assume_unique=False) Docstring: Test whether each element of a 1D array is also present in a second array. Returns a boolean array the same length as `ar1` that is True where an element of `ar1` is in `ar2` and False otherwise. Parameters ---------- ar1 : array_like, shape (M,) Input array. ar2 : array_like The values against which to test each value of `ar1`. assume_unique : bool, optional If True, the input arrays are both assumed to be unique, which can speed up the calculation. Default is False. Returns ------- mask : ndarray of bools, shape(M,) The values `ar1[mask]` are in `ar2`. See Also -------- numpy.lib.arraysetops : Module with a number of other functions for performing set operations on arrays.
participants (2)
-
James Bullard
-
Zachary Pincus