【发布时间】:2018-08-09 05:18:24
【问题描述】:
我想在不使用 Python 编写“If 语句”的情况下进行内联比较。如果该值满足阈值条件,则应保持不变。如果不是,则该值应设置为 0。
在 Python 中,我似乎不允许将布尔运算符直接应用于列表。在 Matlab 中,'True' 给出 '1' 而 'False' 在数组运算中给出 0 很方便。这类似于 matlab,但在 python 中不起作用(也许与 numpy 一起使用?)。伪代码示例:
a = [1.5, 1.3, -1.4, -1.2]
a_test_positive = a>0 # Gives [1, 1, 0, 0]
positive_a_only = a.*a>0
想要的结果:
positive_a_only>> [1.5, 1.3, 0, 0]
在 python 中最好的方法是什么?
【问题讨论】:
标签: python arrays operators threshold