NumPy ndarray 的三元运算符?
NumPy 有三元运算符吗?例如,在 R 中,有一个向量化的 if-else
函数:
> ifelse(1:10 < 3,"a","b")
[1] "a" "a" "b" "b" "b" "b" "b" "b" "b" "b"
NumPy 中有等效的函数吗?
Does NumPy have a ternary operator? For instance, in R there is a vectorized if-else
function:
> ifelse(1:10 < 3,"a","b")
[1] "a" "a" "b" "b" "b" "b" "b" "b" "b" "b"
Is there anything equivalent in NumPy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找
numpy.where()
:NumPy 甚至具有泛化(将 0、1、2 等映射到值,而不是仅映射 True 和 False):
numpy.choose()
< /a>.You are looking for
numpy.where()
:NumPy even has a generalization (that maps 0, 1, 2, etc. to values, instead of mapping only True and False):
numpy.choose()
.