python - 替换 numpy 数组中的负值

标签 python numpy

有没有一种简单的方法可以用 0 替换数组中的所有负值?

我对如何使用 NumPy 数组进行了完整的了解。

例如

a = array([1, 2, 3, -4, 5])

我要回去

[1, 2, 3, 0, 5]

a < 0给出:

[False, False, False, True, False]

这就是我卡住的地方——如何使用这个数组来修改原始数组。

最佳答案

你已经成功了一半。试试:

In [4]: a[a < 0] = 0

In [5]: a
Out[5]: array([1, 2, 3, 0, 5])

关于python - 替换 numpy 数组中的负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10335090/

相关文章:

python - Numpy.where : very slow with conditions from two different arrays

python - 已安装 OpenCV 但在 IDLE 中未找到模块 "cv2"

python - 我如何判断一个 numpy bool 数组是否只包含一个 `True` block ?

python - wxPython - 使用 wx python 模块获取现有的 windowDC/句柄?

使用 IMAP 的 Python Outlook 电子邮件识别和阅读链接

python - 在 Python 中实现广义生日悖论

python - 检测 2 个数据帧中的重叠值

python - 使用 F2PY 处理 Python 中的 Fortran 字符数组

python - 如何在 pandas DataFrame 中查找特定列的重复行,并通过添加计数器来修改值?

python - 赋值前引用的变量 Python