繁体   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