将负值交换为 numpy 数组中的零 Python

Swaping the negative values into zeroes in a numpy array Python

如何编写将 a 的所有负值交换为零的代码。

import numpy as np 

a = np.array([12,12,123,4,-4,0.15,-100])

预期输出:

[12,12,123,4,0,0.15,0]

你可以使用 numpy 的 clip 函数

https://numpy.org/doc/stable/reference/generated/numpy.clip.html

a.clip(min = 0)

试试这个:

format_number = lambda n: n if n % 1 else int(n)
a = list(map(lambda n: 0 if n < 0 else format_number(n), a))
print(a)

输出:

[12, 12, 123, 4, 0, 0.15, 0]