如何獲得python中提供的值的下一個最小值?有沒有內置的功能?獲取python中的數字列表中的最小值
>>>num_list=[1,2,3,4]
>>> min(num_list)
1
>>> max(num_list)
4
我怎樣才能找到下一個最低到3或下一個最大到2?預期的結果是2和3。
如何獲得python中提供的值的下一個最小值?有沒有內置的功能?獲取python中的數字列表中的最小值
>>>num_list=[1,2,3,4]
>>> min(num_list)
1
>>> max(num_list)
4
我怎樣才能找到下一個最低到3或下一個最大到2?預期的結果是2和3。
TL; DR要麼min(n for n in my_list if n>lower_bound)
或max(n for n in my_list if n<upper_bound)
的簡易求更快的替代直接最小或即時最大是numpy
>>> import numpy as np
>>> np.random.seed(10)
>>> a = np.random.random(10000)
>>> a[a>0.7].min()
0.69999533217645671
>>> a[a<0.7].max()
0.70003449227846715
如果您使用的是numpy
機械 ,並希望與清單只是處理
>>> a = list(a)
,那麼你可以使用min
和max
建宏隨着發電機 表達
>>> min(n for n in a if n>0.7)
0.69999533217645671
>>> max(n for n in a if n<0.7)
0.70003449227846715
>>>
使用您列出不舒服,當然,相同的結果,但要注意性能有所不同:使用ipython
和%timeit
來獲得時間,我有871微秒使用numpy
和13.8毫秒使用常規列出前面例子的100000個元素數組/列表。
HTH,僑
郵政Scriptum
在回答這些解決方案都爲O(n),與O相比(N log n)的的使用排序方法 - - 另外,對於大數據集numpy
方法應該(斜體,因爲我手邊沒有測試...)會受到一個小乘法因子的影響。
'解決了我的問題,使用'min(n爲n,如果n> m)。如果列表不是由'np.random.seed(n)'生成的,我可以將常規列表轉換爲輸入'np.random.random()'嗎? – cutteeth 2015-04-07 06:04:05
在我的理解中,你問:「我有一個_regular_列表,並且出於速度的原因,我想使用'numpy''...但在我看來,你不熟悉'numpy',所以我的建議是「留在常規列表中」。如果你想學習相當多的新東西,這裏是配方:''np',my_list = list_computing_function()','my_array = np.array(my_list)',最後'nearmin5 = my_array [ my_array> 5] .min()'但是請不要盲目應用它,在你的上下文中它甚至可能比列表解決方案慢。 – gboffi 2015-04-07 06:52:03
可以嗎?請刪除答案的numpy部分?每當我收到關於相同的通知時,我都會檢查這個答案。我認爲最好去除numpy部分,或者將它移動到最小和最大部分之下,這是問題的適當答案。謝謝 :) – cutteeth 2017-05-07 18:24:27
您可以使用sorted
:
>>> l=sorted(num_list,reverse=True)
>>> l[l.index(3)+1]
2
但作爲一個更Python的方式Frerich拉貝在評論說,你不需要瘡整個列表,你可以找到那些低於3元的最大值:
>>> max(i for i in num_list if i<3)
2
而對於2之後下一個最大的,你可以使用min
:
>>> min(i for i in num_list if i>2)
3
您可以使用以下方法:
num_list = [1,2,3,4]
inds = sorted(range(len(num_list)), key=lambda k: num_list[k])
然後,INDS [1]將包含一個最低的元素的索引等。 此外,您可以使用下面的代碼不排序:
minv = min(num_list)
nmin = min(nm for nm in num_list if nm > minv)
maxv = max(num_list)
nmax = max(nm for nm in num_list if nm < maxv)
使用'sorted'是矯枉過正,第二個提議是O(n^2)。 – 2015-04-06 13:44:02
你是對的,我糾正了我的答案。 – kvorobiev 2015-04-06 13:45:39
下一個最低至3:
max([x for x in num_list if x < 3])
下一個最大的以2:
min([x for x in num_list if x > 2])
這裏你不需要列表解析,即只有'max(x對於num_list中的x,如果x <3)'足夠(並且更高效)。 – 2015-04-06 13:21:14
所提供的答案是好的,但如果我可以提出一個建議 - 如果有時候可以重複這些值的話,比如
num_list = [2, 2, 4, 4, 6, 7, 8, 9]
...等等,只是對列表進行排序並獲得第一個索引可能不是您要查找的內容。
使其通過一個set()
第一,你會確保每個條目是單:
def sorted_ordered_list(sequence):
return sorted(list(set(sequence)))
那麼你可以只指數返回list
爲無論你正在尋找,從價值最低在索引0到最高。
實施例:
>>> my_list = [1, 5, 4, 3, 6, 3, 8, 3, 6, 7, 4, 2, 6, 7, 9, 8, 8]
>>> sorted_ordered_list(my_list)
[1, 2, 3, 4, 5, 6, 7, 8, 9] # now index the list for the desired value
>>>
這是一種方式,但昂貴。考慮到原始問題可以在線性時間和固定空間內解決。 – wsysuper 2015-04-07 10:33:33
當然,但它是一個單用途,使用零但內置,有點像OP要求 – 2015-04-07 13:40:51
我明白你的問題是標記[下限]和[上界。如果您的列表已排序,則Python具有等效的C++ <算法>的lower_bound
和upper_bound
。他們在bisect
模塊中。他們返回一系列特定值的開始和結束後的索引。
In [1]: import bisect
In [2]: A = [0, 1, 3, 3, 5]
In [3]: A[bisect.bisect_left(A, 3)-1]
Out[3]: 1
In [4]: A[bisect.bisect_right(A, 3)]
Out[4]: 5
我很抱歉的延遲答覆,但我可以知道複雜性,請嗎? – cutteeth 2017-05-07 18:17:35
@cutteeth O(log n),但您的列表必須已經排序。 – 2017-05-08 00:59:23
使用heapq.nlargest
和heapq.nsmallest
import heapq
num_list = [1, 2, 3, 4]
heapq.nlargest(2, num_list)
heapq.nsmallest(2, num_list)
#>>> [4, 3]
#>>> [1, 2]
是列表排序? – 2015-04-06 13:10:30