【发布时间】:2025-12-21 06:00:07
【问题描述】:
我有以下代码:
def getEmptySquares(self):
emptySquares=deque([])
for i in range(self.grid.shape[0]):
for j in range(self.grid.shape[1]):
if np.array([i,j]) not in dequeList:
emptySquares.append(np.array([i,j]))
print(emptySquares)
其中网格是一个 numpy 数组。
dequeList 变量的一个例子是:
deque([array([5, 7]), array([6, 7]), array([6, 6]), array([6, 5]), array([6, 4]), array([5, 4])])
运行此函数时出现以下错误:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
为什么会这样?
【问题讨论】:
-
@AMC 虽然这两个问题之间存在一些重叠,但您链接的问题并未解决为什么在 OP 问题的上下文中会发生此错误,即在集合中搜索数组时。
标签: python error-handling deque valueerror