[Numpy-discussion] howto exclude elements from sorted array which are present in other sorted array

Charles R Harris charlesr.harris at gmail.com
Tue May 29 09:17:01 EDT 2007


On 5/29/07, dmitrey <openopt at ukr.net> wrote:
>
> hi all,
> I have 2 sorted arrays arr1 & arr2 (ascending order, for example
> [1,2,5,8] and [3,5], but length may be thousands, so I need simpliest
> way, not costly).
> Howto obtain arr3 that consists of elements present in arr1 but absent
> in arr2?
> Of course, I can write something by myself, like
> arr3 = [x for x in arr1 if not x in arr2]
> but maybe already written routines are present in numpy? (that use sort)
>
> Usually arr2 is much more small than arr1, typical former size is 10-20
> and typical latter size can be 1000-10000



count = arr2.searchsorted(arr1, side='right') - arr2.searchsorted(arr1,
side='left')
arr3 = arr1[count == 0]

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20070529/4a0313d6/attachment.html>


More information about the NumPy-Discussion mailing list