0

假设一个列表列表,如下所示:

lists=[[3,2,1,4],[6,4,2,8],[9,6,3,12]]

如果我想按lists[0]它排序,应该这样排序:

lists=[[1,2,3,4],[2,4,6,8],[3,6,9,12]] #can be another variable

有一个包含三个列表的列表:

list1,list2,list3=(list(t) for t in zip(*sorted(zip(lists[0], lists[1],lists[2]))))

但这是一个特定的解决方案,列表中的 n 个列表可能更通用。变量是这样创建
的:lists

lists = [[] for x in xrange(n)]

并附加了这些值。

实际上所有值都是数字(浮点数或整数),但这可能会更好地解释它。

lists=[[3,2,1,4],["three","two","one","four"],["tres","dos","uno","cuatro"]]

我想知道如何排序并最终得到这个:

[[1,2,3,4],["one","two","three","four"],["uno","dos","tres","cuatro"]]
4

1 回答 1

4

这可能是最好使用 Schwartzian 变换的不太可能的情况

>>> lists=[[3,2,1,4],["three","two","one","four"],["tres","dos","uno","cuatro"]]
>>> [[x for i, x in sorted(zip(lists[0], sublist))] for sublist in lists]
[[1, 2, 3, 4], ['one', 'two', 'three', 'four'], ['uno', 'dos', 'tres', 'cuatro']]
于 2013-04-21T21:42:53.893 回答