我有一个k元素的元组列表。我想对元素0进行排序,然后对元素1进行排序,依此类推。我用谷歌搜索,但我仍然不知道该怎么做。会是这样吗?
list.sort(key = lambda x : (x[0], x[1], ...., x[k-1])
特别是,我想使用不同的条件进行排序,例如,从元素0降序,从元素1升序,依此类推。
我有一个k元素的元组列表。我想对元素0进行排序,然后对元素1进行排序,依此类推。我用谷歌搜索,但我仍然不知道该怎么做。会是这样吗?
list.sort(key = lambda x : (x[0], x[1], ...., x[k-1])
特别是,我想使用不同的条件进行排序,例如,从元素0降序,从元素1升序,依此类推。
Since python's
sort
is stable for versions after 2.2 (or perhaps 2.3), the easiest implementation I can think of is a serial repetition ofsort
using a series ofindex, reverse_value
tuples:这会进行多次,因此就恒定的时间成本而言可能效率低下,但就渐进复杂度而言仍为O(nlogn)。
If the sort order for indices is truly
0, 1... n-1, n
for a list of n-sized tuples as shown in your example, then all you need is a sequence of True and False to denote whether you wantreverse
or not, and you can useenumerate
to add the index.虽然原始代码允许按索引的任何顺序进行排序的灵活性。
Incidentally, this "sequence of sorts" method is recommended in the Python Sorting HOWTO with minor modifications.
编辑 如果您不需要按某些索引升序排序,而按其他索引降序排序,则
将按索引1排序,然后按索引3排序关系,按索引5进一步排序关系。但是,更改每个索引的升序/降序并不容易。
因此,我假设您要对tuple_0进行升序排序,然后对tuple_1进行降序排序,依此类推。有点冗长,但这是您可能正在寻找的:
This is actually using the tuple as its own sort key. In other words, the same thing as calling
sort()
with no argument.如果我假设您简化了问题,并且实际元素实际上并不是按照您想要的排序顺序排序(例如,最后一个值的优先级最高),则可以使用相同的技术,但是可以对排序基于优先级的密钥:
通常,这是一个非常方便的技巧,即使在C ++之类的其他语言中(如果您使用的是库):当您要按优先级不同的多个成员对对象列表进行排序时,也可以通过创建一个按照优先级顺序包含所有相关成员的元组。
Final trick (this one is off topic, but it may help you at some point): When using a library that doesn't support the idea of "sort by" keys, you can usually get the same effect by building a list that contains the sort-key. So, instead of sorting a list of
Obj
, you would construct then sort a list of tuples:(ObjSortKey, Obj)
. Also, just inserting the objects into a sorted set will work, if they sort key is unique. (The sort key would be the index, in that case.)