Numpy:如何检查数组是否包含某些数字?

时间:2021-10-19 18:20:35

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

例如:a = np。数组([123,412,444])和b = np。阵列((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.

我想知道a是否包含了b中的所有元素,这有一个简单的操作吗?在这种情况下,这是不正确的。

4 个解决方案

#1


10  

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

您可以使用设置差异来确定您要查找的内容。Numpy有一个内置的函数叫Numpy。setdiff1d(ar1 ar2):

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

返回ar1中没有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.

对于这种情况,你要做一个集合差你要从b中减去a得到一个包含b中不包含a的元素的数组,然后你可以检查它是否为空。如你所见,输出是312,这是a中的一个元素,但不是b中的;它的长度现在大于0,因此b中有一些元素没有出现在a中。

#2


12  

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:

>>> 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.

循环在numpy数组上是懒散的,应该避免。

#3


2  

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

这意味着你要检查b的每个元素是否都包含在a中,in1d会这样做:

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

#4


-1  

you could do:

你能做的:

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

#1


10  

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

您可以使用设置差异来确定您要查找的内容。Numpy有一个内置的函数叫Numpy。setdiff1d(ar1 ar2):

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

返回ar1中没有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.

对于这种情况,你要做一个集合差你要从b中减去a得到一个包含b中不包含a的元素的数组,然后你可以检查它是否为空。如你所见,输出是312,这是a中的一个元素,但不是b中的;它的长度现在大于0,因此b中有一些元素没有出现在a中。

#2


12  

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:

>>> 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.

循环在numpy数组上是懒散的,应该避免。

#3


2  

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

这意味着你要检查b的每个元素是否都包含在a中,in1d会这样做:

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

#4


-1  

you could do:

你能做的:

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