Find min value in array > 0

前端 未结 7 1949
夕颜
夕颜 2021-02-07 05:49

I am looking to find the lowest positive value in an array and its position in the list. If a value within the list is duplicated, only the FIRST instance is of interest. This i

7条回答
  •  不思量自难忘°
    2021-02-07 06:19

    You can use a generator expression with min. This will set m as the minimum value in a that is greater than 0. It then uses list.index to find the index of the first time this value appears.

    a = [4, 8, 0, 1, 5]
    
    m = min(i for i in a if i > 0)
    
    print("Position:", a.index(m))
    print("Value:", m)
    # Position: 3
    # Value: 1
    

提交回复
热议问题