我真的很困惑为什么会出现这个错误。以下是我的代码:
import numpy as np
x = np.array([0, 0])
y = np.array([10, 10])
a = np.array([1, 6])
b = np.array([3, 7])
points = [x, y, a, b]
max_pair = [x, y]
other_pairs = [p for p in points if p not in max_pair]
>>>ValueError: The truth value of an array with more than one element is ambiguous.
Use a.any() or a.all()
(a not in max_paix)
>>>ValueError: The truth ...
让我困惑的是,以下内容可以正常工作:
points = [[1, 2], [3, 4], [5, 7]]
max_pair = [[1, 2], [5, 6]]
other_pairs = [p for p in points if p not in max_pair]
>>>[[3, 4], [5, 7]]
([5, 6] not in max_pair)
>>>False
当使用numpy数组时,为什么会发生这种情况?使用 not in/in
是否含糊不清?
使用any()\all()
的正确语法是什么?
not in
)可以通过[p for p in points if all(any(p!=q) for q in max_pair)]
来获得。 - igavriil