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

How to check that List[np.ndarray] contains an np.ndarray?

我有一个 np.ndarray 和一个包含多个 np.ndarray 元素的 list。 我想检查我的 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()

我除了有一些方法可以检查 b 包含 a 而无需更改 ab 类型。

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

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