Find the min/max excluding zeros in a numpy array (or a tuple) in python

前端 未结 6 822
野性不改
野性不改 2020-12-08 09:36

I have an array. The valid values are not zero (either positive or negetive). I want to find the minimum and maximum within the array which should not take zeros into accoun

6条回答
  •  星月不相逢
    2020-12-08 09:47

    You could use a generator expression to filter out the zeros:

    array = [-2, 0, -4, 0, -3, -2]
    max(x for x in array if x != 0)
    

提交回复
热议问题