[Numpy-discussion] Numpy array in iterable

Robert Cimrman cimrman3 at ntc.zcu.cz
Thu Mar 5 07:28:14 EST 2009


Kim Hansen wrote:
> Hi again
> 
> It turned out not to be quite good enough as is, as it requires unique
> values for both arrays. Whereas this is often true for the second
> argument, it is never true for the first argument in my use case, and
> I struggled with that for some time until i realized I could use
> unique1d with the rever_index optional parameter set True
> 
> def ismember(totest, members)
>         """
>         A setmember1d, which works for totest arrays with duplicate values
>         """
>         uniques_in_test, rev_idx = unique1d(totest, return_inverse=True)
>         uniques_in_members_mask = setmember1d(uniques_in_test, members)
>         # Use this instead is members is not unique
>         # uniques_in_members_mask = setmember1d(uniques_in_test,
> unique1d(members))
>         return uniques_in_members_mask[rev_idx]
> 
> I saw someone else providing an alternative implementation of this,
> which was longer and included a loop. I do not know which is the most
> efficient one, but I understand this one better.
> 
> -- Slaunger

I have added your implementation to 
http://projects.scipy.org/numpy/ticket/1036 - is it ok with you to add 
the function eventually into arraysetops.py, under the numpy (BSD) license?

cheers,
r.



More information about the NumPy-Discussion mailing list