如何检查List [np.ndarray]是否包含np.ndarray?

时间:2019-03-28 13:41:15

标签: python-3.x numpy numpy-ndarray

我有一个np.ndarray和一个带有几个list元素的np.ndarray。 我想检查我的list是否包含具体的np.ndarray

我尝试使用in运算符,但得到了ValueError

>>> import numpy as np
>>> a = np.asarray([1, 2, 3])
>>> b = [np.asarray([1, 2, 3]), np.asarray([2, 2, 2])]
>>> a in b  # attempt to check №1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element
is ambiguous. Use a.any() or a.all()
>>> any(x == a for x in b)  # attempt to check №2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: The truth value of an array with more than one element
is ambiguous. Use a.any() or a.all()

除了在不更改ba类型的情况下可以用某种方法检查a包含b的情况之外,我是其他人。

1 个答案:

答案 0 :(得分:0)

您可以使用numpy的标准函数numpy.array_equal(a1,a2)

for i in b: if(numpy.array_equal(a,i)): print("found")