11

For example: I have a = np.array([123, 412, 444]) and b = np.array([123, 321])

I want to know if a contains all the elements in b. Is there a simple operation for this? In this case that would not be true.

2
  • Do you mean unique elements? a = [3,3,4], b = [3,4,5], are all elements of a contained in b? Commented May 12, 2012 at 16:56
  • No I meant that all the elements in b has to be in a. The answers below where what I was looking for. Thanks Commented May 12, 2012 at 17:03

4 Answers 4

14

You could alway use a set:

>>> a = numpy.array([123, 412, 444])
>>> b = numpy.array([123, 321])
>>> set(b) in set(a)
False

Or with newer versions of numpy:

>>> numpy.in1d(b,a)
array([ True, False], dtype=bool)

If you want just 'the answer' rather than an array:

>>> numpy.in1d(b,a).all()
False

Or (least desirable):

>>> numpy.array([x in a for x in b]) 
array([ True, False], dtype=bool)

Looping is slowish on numpy arrays and should be avoided.

Sign up to request clarification or add additional context in comments.

1 Comment

set(b) in set(a) probably doesn't do what you want, that would be set.is[super|sub]set.
11

You can use set difference to determine what you are looking for. Numpy has a built-in function called numpy.setdiff1d(ar1, ar2):

Return the sorted, unique values in ar1 that are not in ar2.

Example for your case:

>>> a = np.array([123, 412, 444])
>>> b = np.array([123, 321])
>>> diff = np.setdiff1d(b, a)
>>> print diff
array([321])
>>> if diff.size:
>>>    print "Not passed"

So for your case, you would do a set difference you would subtract a from b and obtain an array with elements in b which are not in a. Then you can check if that was empty or not. As you can see, the output is 312, which is an entry present in a but not in b; the length of it is now larger then zero, therefore there were elements in b which were not present in a.

3 Comments

@luffe if I answered your question, please mark it as an answer.
@petr better to check if diff instead
@petr, do you mean 321 instead of 312 for array elements which are in b but not in a?
5

that means you want to check if each element of b is contained in a. in1d does that:

from numpy import array, in1d
a = array([123, 412, 444])
b = array([123, 321])
print in1d(b, a).all()

Update from 2021: nowadays np.isin is recommended

Comments

-1

you could do:

 a = an_array
 b = another_array
 for i in b:
    if i not in a:
        return False
return True

3 Comments

That is not valid Python. Also, a Python loop on a numpy array is very slow.
how exactly is that invalid python?
Obviously you have not run it in the interpreter. If you had done so you would have discovered your code raises a SyntaxError: 'return' outside function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.