python - 为什么选择numpy数组元素时不能使用AND运算符?
问题描述
我正在尝试这样做:
X = U[(U > lims[0] & U < lims[1])] #U is numpy array
输出:
Traceback (most recent call last):
File "Ha.py", line 19, in <module>
X = U[(U > lims[0] & U < lims[1])]
TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
但
X = U[(U > lims[0])
完美运行。
怎么了?我怎样才能优雅地克服这一点?
解决方案
当您尝试以下操作时:
X = U[(U > lims[0] & U < lims[1])]
Numpy 认为您正在尝试执行按位运算。这是因为按位&
运算的优先级高于条件>
运算。
您应该尝试以下方法:
X = U[(U > lims[0]) & (U < lims[1])]
推荐阅读
- java - 如何为 Selenium Java 自动安装正确版本的 chromedriver
- reactjs - 组件没有像在 React 上那样频繁地更新
- gradle - 在多项目构建中为多个 Gradle 项目贡献代码覆盖率
- qt - 我可以在 TeXstudio 中编写一个宏来选择一个字符吗?
- php - 在 Woocommerce 电子邮件中将收件人添加到“客户发票/订单详细信息”
- c# - 尝试使用此代码在 C# 的 Bot 框架中实现 razorpay 支付网关(链接生成 API)
- java - 如何将 java 变量设置为等于用户输入的任何方程的输出?
- java - 我的简单 Java 程序在 Eclipse 上运行没有问题,但是在我的电脑中找不到 Java
- reactjs - 如何使用 Typescript 为自定义 PopperComponent 输入材质 ui 道具
- javascript - 使用 useEffect() 将 redux 状态放入本地状态不会完全改变本地状态