0
votes

I am looking for an efficient way to check whether all string entries in a certain numpy array are contained in a second numpy array. See the example below. Array_1 would be the minimal animals that should be checked for. The function should return False in case none or part of the animals are contained and True if all three of them (in arbitrary order) are contained.

import numpy as np
array_1 = np.array(['cat', 'dog', 'goat'])

array_2 = np.array(['cat', 'monkey', 'zebra', 'pig', 'goat', 'horse', 'dog'])
array_3 = np.array(['peacock', 'horse', 'zebra', 'pig', 'cat', 'horse', 'dog', 'sheep'])

compare_function(array_1, array_2)

My current solution has definitely too many for loops and if statements inside. I had already a look on numpy array logical operations but it seemed to me that those are not perfect, because my arrays do not have the same length and not necessarily the same animal order!?

2
perfect, exactly what I was locking for. Post it as an answer, maybe together with np.mean(np.in1d(array_1, array_2)) == 1 in case that all entries are True and I am going to mark it as the right onecattt84
You can simply use: np.in1d(array_1, array_2).all().Ashwini Chaudhary

2 Answers

3
votes

If you do not mind using set , then you can convert the arrays to set , using the built-in function set() and then do subset check using the less than - < or greater than operator - > . The less than operator for example checks whether the set on the left side is a subset of the one of the right side. Vice versa for greater than operator.

Example -

In [8]: import numpy as np

In [9]: array_1 = np.array(['cat', 'dog', 'goat'])

In [10]:

In [10]: array_2 = np.array(['cat', 'monkey', 'zebra', 'pig', 'goat', 'horse', 'dog'])

In [11]: array_3 = np.array(['peacock', 'horse', 'zebra', 'pig', 'cat', 'horse', 'dog', 'sheep'])

In [12]:

In [12]: set(array_1) <= set(array_2)
Out[12]: True

In [13]: set(array_1) <= set(array_3)
Out[13]: False

There is also method called - .issubset() , which you can use to check for subsets.

Example -

In [17]: set(array_1).issubset(array_2)
Out[17]: True
0
votes

Probably the simplest way to achieve this is to convert the arrays into sets and use the issubset method.