簡體   English   中英

使用其他數組 python 的排序對 arrays 列表進行排序

[英]Sort a list of arrays using the sort of other array python

我有兩個列表:二維數組列表和生成時間,即 integer。 它們的長度為 N 並且都“同樣無序”。 因此,通過使用索引對列表“時間”進行排序,我可以對二維數組列表進行排序。

我想做類似的事情:

ordered_list_of_arrays = np.asarray(disordered_list).argsort(np.asarray(time))
ordered_time = np.asarray(time).sort()

另一種選擇是將其保留為列表:

ordered_arrays = disordered_list[np.argsort(np.asarray(time))]
TypeError: only integer scalar arrays can be converted to a scalar index

通過迭代 np.argsort(time) 我可以對我的disordered_list進行排序,但我想知道是否有更好的選擇或者哪個是最好的。

謝謝

創建一個索引數組並根據 time_list 的值對其進行排序。 然后使用這些索引來構建 arrays 的排序版本。

代碼:

def sorted_lists(time_list, array_list):
    sorted_indices = sorted(range(len(time_list)), key=lambda i: time_list[i])
    sorted_time_list = [time_list[i] for i in sorted_indices]
    sorted_array_list = [array_list[i] for i in sorted_indices]
    return sorted_time_list, sorted_array_list

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM