【发布时间】:2015-03-06 22:05:33
【问题描述】:
我真的很困惑为什么会出现这个错误。这是我的代码:
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() 的正确语法是什么?
【问题讨论】: