[Numpy-discussion] Array2 subset of array1

Nathaniel Smith njs at pobox.com
Tue Aug 5 09:15:18 EDT 2014

On Tue, Aug 5, 2014 at 1:58 PM, Jurgens de Bruin <debruinjj at gmail.com> wrote:
> Hi,
> I am new to numpy so any help would be greatly appreciated.
> I have two arrays:
>     array1 = np.arange(1,100+1)
>     array2 = np.arange(1,50+1)
> How can I calculate/determine if array2 is a subset of array1 (falls within
> array 1)
> Something like : array2 in array1 = TRUE for the case above.

Does this work?
  np.in1d(array2, array1)


(Note that while in1d does the best it can, set operations on arrays
will usually be slower than if you used a more appropriate data type
like 'set' or 'dict'.)


Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh

More information about the NumPy-Discussion mailing list