如何在 python 中找到列表数组的最小点

How find minimum point of array of lists in python

    def get_minima(array):

sdiff = np.diff(np.sign(np.diff(array)))
rising_1 = (sdiff == 2) 
rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1) 
rising_all = rising_1 
rising_all[1:] = rising_all[1:] | rising_2 
min_ind = np.where(rising_all)[0] + 1 
minima = list(zip(min_ind, array[min_ind]))

return sorted(minima, key=lambda x: x[1])

通过运行这段代码和我拥有的数据数组,它产生:

[(59, 7.958373616052042e-10),
 (69, 6.5364637051479655e-09),
 (105, 1.0748381102806489e-08),
 (88, 2.953895857338913e-07),
 (27, 9.083111768048306e-07)]

太棒了 - 它是我数据集中的所有最小值。但我只需要存储最小值——在这个特定示例中是 (59, 7.958373616052042e-10) 点。我不知道该怎么做。我尝试了一些使用 np.amin 并进行布尔比较的东西,但我对符号和语法感到很困惑,因为现在它是一个列表数组,我以前从未真正使用过它。
感谢任何帮助!

嗯,看起来太简单了,但是你试过了吗?

minima = list(zip(min_ind, array[min_ind]))
minima.sort(key=lambda x: x[1])
return minima[0]

无需对所有最小值进行排序,您可以只得到最低的一对:

def get_minima(array):
    sdiff = np.diff(np.sign(np.diff(array)))
    rising_1 = (sdiff == 2) 
    rising_2 = (sdiff[:-1] == 1) & (sdiff[1:] == 1) 
    rising_all = rising_1 
    rising_all[1:] = rising_all[1:] | rising_2 
    min_ind = np.where(rising_all)[0] + 1 
    minima = list(zip(min_ind, array[min_ind]))
    return min(minima, key=lambda pair: pair[1])

例如:

minima = [(59, 7.958373616052042e-10),
 (69, 6.5364637051479655e-09),
 (105, 1.0748381102806489e-08),
 (88, 2.953895857338913e-07),
 (27, 9.083111768048306e-07)]

minimum = min(minima, key=lambda pair: pair[1])
print(minimum)

>>> (59, 7.958373616052042e-10)