Python 在数组中查找最小值 > 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27966757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:33:00  来源:igfitidea点击:

Find min value in array > 0

pythonlistsearchmin

提问by user3001499

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.

我正在寻找数组中最低的正值及其在列表中的位置。如果列表中的值重复,则只有 FIRST 实例值得关注。这就是我所拥有的,它可以满足我的要求,但包括 0。

print "Position:", myArray.index(min(myArray))
print "Value:", min(myArray)

for example, as it stands if,

例如,就目前而言,如果,

myArray = [4, 8, 0, 1, 5]

myArray = [4, 8, 0, 1, 5]

then Position: 2, Value: 0

然后位置:2,值:0

I want it to present position: 3, value: 1

我希望它呈现位置:3,值:1

采纳答案by Ffisegydd

You can use a generator expressionwith min. This will set mas the minimum value in athat is greater than 0. It then uses list.indexto find the index of the first time this value appears.

您可以将生成器表达式min. 这将设置ma大于 0的最小值。然后用于list.index查找该值第一次出现的索引。

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

回答by markcial

add a filter then :

添加一个过滤器然后:

myArray = [4, 8, 0, 1, 5]
result = min(filter(lambda x: x > 0, myArray))
print result # 1
print myArray.index(result) # 3

回答by sokoli

def find_min_position(array):
    plus_array = [elem for elem in array if elem > 0]
    min_elem = min(plus_array)
    return min_elem, array.index(min_elem)

In : find_min_position([4, 8, 0, 1, 5])
Out: (1, 3)

回答by thefourtheye

You can use the minfunction and enumeratefunction, like this

你可以使用min函数和enumerate函数,像这样

result = min(enumerate(a), key=lambda x: x[1] if x[1] > 0 else float('inf'))
print("Position : {}, Value : {}".format(*result)
# Position : 3, Value : 1

This makes sure that, if the value is greater than 0, then use that value for the minimum value comparison otherwise use the maximum possible value (float('inf')).

这确保,如果值大于0,则使用该值进行最小值比较,否则使用可能的最大值 ( float('inf'))。

Since we iterate along with the actual index of the items, we don't have to find the actual index with another loop.

由于我们与项目的实际索引一起迭代,因此我们不必使用另一个循环来查找实际索引。

回答by neil

Here is another way of doing it with a generator expression. Note how the values coming from enumerate (a and b) are swapped in the tuple to sort correctly.

这是使用生成器表达式执行此操作的另一种方法。请注意来自 enumerate (a 和 b) 的值如何在元组中交换以正确排序。

value,position = min(((b,a) for a,b in enumerate(myArray) if b>0), default=(None,None))

The default argument will be returned when the generator expression returns nothing (i.e. there are no items greater than 0). The default can be set to whatever makes sense in the surrounding program logic - here returning Nonewill allow you to test with either if value:or if position:

当生成器表达式不返回任何内容(即没有大于 0 的项)时,将返回默认参数。默认值可以设置为周围程序逻辑中有意义的任何值 - 这里返回None将允许您使用if value:if position:

回答by sramij

import numpy as np

x = np.array([1,2,0,5,10])
x = np.extract(x>0,x)
min_index = np.amin(x)
min_value = np.argmin(x)

回答by JHaps

the complicated / algorithmic way:

复杂/算法方式:

int min = array[0], i = 1
list smallest //list of indexes of the smallest element 

// find the first element greater than 0
while (min <= 0 and i < array.length) {
    min = array[i]
    i++
}

// find the first instance of the smallest element greater than 0
while (i < array.length) {
    if (array[i] < min and array[i] > 0) {
        clear the list
        min = array[i]
        list.append(i)
    }
    else if (array[i] == min) {
        list.append(i)
    }
    i++;
}

the first instance of the smallest element greater than 0 is now the first element that you added to the list.

大于 0 的最小元素的第一个实例现在是您添加到列表中的第一个元素。

edit: you'll also have a list of every index of the smallest value. Some simple checks can tell you if there are no elements in the array greater than 0, or if the list is empty, etc.

编辑:您还将拥有一个包含最小值的每个索引的列表。一些简单的检查可以告诉您数组中是否没有大于 0 的元素,或者列表是否为空等。