[SciPy-user] Equivalent to 'match' function in R?

Yannick Copin yannick.copin@laposte....
Thu Jul 24 10:09:22 CDT 2008


Wes McKinney <wesmckinn <at> gmail.com> writes:
> Before I reinvent the wheel, was curious if there was an equivalent function
elsewhere already. If not then some Cython/Pyrex is in order.Thanks,Wes

For a similar purpose (with the handling of non-common items), I used:

def find_indices(big, small):
    """Find indices in big array of elements of small arrays. Set index=-1
    elements of small that cannot be found in big."""

    idx = np.array([ np.argmax(big==s) for s in small ])
    # Mark unfound value indices (beware: -1 is a valid index!)
    idx[big[idx]!=small] = -1

    return idx

In [63]: all_data = np.array([[1,2,3,4,5], [12,19,27,38,51]]).T
In [64]: sub_data = np.array([[3,5,1], [nan,nan,nan]]).T
In [67]: match_ind = find_indices(all_data[:,0],sub_data[:,0])
In [68]: sub_data[:,1] = all_data[match_ind,1]
In [69]: sub_data
Out[69]: 
array([[  3.,  27.],
       [  5.,  51.],
       [  1.,  12.]])

Cheers.




More information about the SciPy-user mailing list