python:numpy.where 出错

标签 python arrays numpy where-clause

我正在尝试使用 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() 重新表述它。我无法获得正确的语法。

最佳答案

您可以使用括号和 &np.logical_and 而不是 and 来构建您的条件:

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

或:

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

关于python:numpy.where 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26405751/

相关文章:

python - 使用 distutil 将 list 嵌入 C 扩展 dll

python - for循环,分割字符串,将部分字符串保存到新列表,IndexError : list index out of range - works for one part of string and not the other

jquery - ColdFusion,如何将字符串转换为数组?

python - 使用 numpy 列表到矩阵转换

python - 在安装 Python 3.4 的同时为 Python 2.7 安装 numpy?

python - 剥离 pandas 数据框并提取每次股票交易的列表

python - NumPy:新旧数据描述符的大小不匹配

arrays - 如何使用openmp并行化数组元素的移动

Javascript - 如何将数组中对象的第一个元素指定为另一个数组的键

Python:numpy.histogram 绘图