Replace negative values in an numpy array

前端 未结 5 935
小蘑菇
小蘑菇 2020-12-04 20:47

Is there a simple way of replacing all negative values in an array with 0?

I\'m having a complete block on how to do it using a NumPy array.

E.g.



        
5条回答
  •  [愿得一人]
    2020-12-04 21:08

    Another minimalist Python solution without using numpy:

    [0 if i < 0 else i for i in a]
    

    No need to define any extra functions.

    a = [1, 2, 3, -4, -5.23, 6]
    [0 if i < 0 else i for i in a]
    

    yields:

    [1, 2, 3, 0, 0, 6]
    

提交回复
热议问题