python - Numpy bool 逻辑(检查元素是否不是什么)

标签 python arrays numpy

当我运行if field[x,y,z] != [-1,-1,-1]:时显示此错误:

Traceback (most recent call last):
  File "C:\Users\dinos\Desktop\CODE\Render\Camera.py", line 63, in controll
    print(self.snap())
  File "C:\Users\dinos\Desktop\CODE\Render\Camera.py", line 59, in snap
    return "".join(self.breakUpArray(self.pBetweenPlane(),self.ep["ab"]))+"\n"+self.getInfo()
  File "C:\Users\dinos\Desktop\CODE\Render\Camera.py", line 42, in pBetweenPlane
    if self.field.field[p2[0],p2[1],p2[2]] != self.field.background:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

“field”是一个用 np.full([100, 100, 100, 3], -1) 创建的 4d-np 数组。我知道错误消息确实给出了提示,但我如何检查 if field[x,y,z] is not [-1,-1,-1]

最佳答案

np.array([-1, -1, -1]) == [-1, -1, -1] 计算结果为 [True, True, True] as numpy 按索引比较它们

你可能想要if not all(field[x,y,z] == [-1,-1,-1])

关于python - Numpy bool 逻辑(检查元素是否不是什么),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64921164/

相关文章:

python - 从同一目录检索多个文件

java - 将 String 转换为 readUTF() 期望的格式

python - Numpy 创建具有特定位置值的矩阵

Python GridSearchCV 索引 xxxxx 超出大小 xxxxxx 的范围

python - 极坐标 DataFrame 行之间的分割值

arrays - UICollectionView 没有显示我的所有数组

c - 将引号中的字符串传输到 C 中的 ascii2 字节数组

PHP 数组键存在于字符串中

numpy - 分段函数和 numpy 模块的 sympy.lambdify 错误

python - 在 numpy 中执行间接排序,为相同元素保留相同索引