![](/img/trans.png)
[英]In Python, how do you find the index of the first value greater than a threshold in a sorted list?
[英]How do I find the sorted position/index of a list in python?
例如,我有一個列表:
[12, 1205, 102, 6]
我想得到
[1, 3, 2, 0]
因為如果列表已排序,這是它們應該處於的位置。
我怎樣才能在python中實現這一點?
IIUC,你想要排序的等級:
sorted_list = sorted(your_list)
[sorted_list.index(x) for x in your_list]
可以使用 numpy 對列表進行排序:
numpy.argsort([2, 3, 5, 1, 4])
您可以使用 sorted() 函數按排序值的順序獲取索引列表,然后使用它來設置結果列表中的位置:
L = [12, 1205, 102, 6]
P = sorted(range(len(L)),key=L.__getitem__) # positions in sorted order
S = [None]*len(L) # resulting list
for p,i in enumerate(P): S[i]=p # assign position at original indexes
print(S) # [1, 3, 2, 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.