python: numpy.where出错

4 投票
1 回答
1432 浏览
提问于 2025-04-27 22:53

我正在尝试使用numpy.where函数,代码是这样的:

x= np.where(segments==1000 and segments == 0)

但是我遇到了一个ValueError错误:

ValueError: The truth value of an array with more than one element is ambiguous. 
Use a.any() or a.all()

我浏览了一些其他的讨论,发现这似乎是正常的情况。不过,我不太确定怎么用numpy.any()来重新写这个。我总是写错语法。

暂无标签

1 个回答

5

你可以用括号和 & 或者 np.logical_and 来构建你的条件,而不是用 and

(segments == 1000) & (segments == 0)

或者:

np.logical_and(segments == 1000, segments == 0)

撰写回答