在数组>中查找最小值0 [英] Find min value in array > 0
本文介绍了在数组>中查找最小值0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找数组中最低的正值及其在列表中的位置.如果列表中的值重复,则仅关注FIRST实例.这就是我想要的,但是要包含0.
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 is what I have which does what I want but includes 0.
print "Position:", myArray.index(min(myArray))
print "Value:", min(myArray)
例如,按现状显示
myArray = [4, 8, 0, 1, 5]
然后位置:2,值:0
then Position: 2, Value: 0
我希望它显示位置:3,值:1
I want it to present position: 3, value: 1
推荐答案
您可以使用生成器表达式和min
.这会将m
设置为a
中的最小值,该值大于0.然后使用list.index
查找该值首次出现的索引.
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
这篇关于在数组>中查找最小值0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文