简体   繁体   English

按First元素对元素列表进行排序,但如果按第二个元素进行等级排序

[英]Sorting a List of Elements by the First element but if Equal sort by the second

I have constructed a list [[13, "b"], [10, "b"], [10, "a",], [4,"c"], [1, "d"]] which is sorted by the first value in the list of two elements. 我已经构建了一个列表[[13, "b"], [10, "b"], [10, "a",], [4,"c"], [1, "d"]]通过两个元素列表中的第一个值。 That is the part I can do. 这是我能做的部分。 What I don't know is if two elements are the same, then I want to sort it alphabetically. 我不知道的是,如果两个元素是相同的,那么我想按字母顺序对它进行排序。 The method I used to sort the first list was reversing a sorted list by the first element. 我用来对第一个列表进行排序的方法是通过第一个元素反转排序列表。 This however makes [10, "b"] come before [10, "a"] but i want "a" before "b". 然而,这使[10, "b"][10, "a"]之前出现[10, "a"]但我想在“b”之前“a”。 Is there a way to write sort by first element in descending order and if a tie, sort alphabetically by the second element? 有没有办法按降序编写第一个元素排序,如果是平局,按第二个元素按字母顺序排序?

This works: 这有效:

>>> li=[[13, "b"], [10, "b"], [10, "a",], [4,"c"], [1, "d"]]
>>> sorted(li,key=lambda sl: (-sl[0],sl[1]))
[[13, 'b'], [10, 'a'], [10, 'b'], [4, 'c'], [1, 'd']]

The sorted function produces a new list. 排序的函数生成一个新列表。 You can also sort the list in place using the sort method: 您还可以使用sort方法对列表进行排序:

>>> li=[[13, "b"], [10, "b"], [10, "a",], [4,"c"], [1, "d"]]
>>> li.sort(key=lambda sl: (-sl[0],sl[1]))
>>> li
[[13, 'b'], [10, 'a'], [10, 'b'], [4, 'c'], [1, 'd']]

You can also do a nested sort (or sort on one key, then the second key) since python uses a stable sort : 你也可以进行嵌套排序(或者对一个键进行排序,然后对第二个键进行排序 ),因为python使用了一个稳定的排序

>>> from operator import itemgetter
>>> li=[[13, "b"], [10, "b"], [10, "a",], [4,"c"], [1, "d"]]
>>> li.sort(key=itemgetter(1))
>>> li
[[10, 'a'], [13, 'b'], [10, 'b'], [4, 'c'], [1, 'd']]
>>> li.sort(key=itemgetter(0),reverse=True)
>>> li
[[13, 'b'], [10, 'a'], [10, 'b'], [4, 'c'], [1, 'd']]

Since there is no need for a lambda in a one element sort, I used the faster operator.itemgetter vs a lambda. 由于在一个元素排序中不需要lambda,我使用了更快的operator.itemgetter与lambda。 Whether two faster sorts are faster than one, I don't know... You can also use a lambda for this method. 两个更快的排序是否比一个更快,我不知道...你也可以使用lambda这个方法。

There is a great sort tutorial that shows many of these idioms. 有一个很好的排序教程 ,显示了许多这些习语。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 。 如果第二个元素相等,如何对子列表的元素进行排序 按第一个元素对子列表进行排序 - Python . How to sort elements of sub-list if second elements are equal sort the sub-lists by first element Python:按第一个元素对元素列表进行排序,但如果相等则按原始索引排序 - Python: Sort list of elements by first element, but if equal sort by original index 按第二个元素对元组列表进行排序,但如果多个元组的第二个元素匹配,则使用第一个元素进行排序 - sorting a list of tuple by second element but if the second element of mutiple tuple matches then sort using the first element 根据长度第一和字母顺序第二对列表中的元素进行排序 - Sort elements in list based on length first and alphabetical order second 如果值等于第二个列表元素,则使用第一个列表的元素创建新列 - Create new column with elements of the first list if value is equal to the second list elements 如何使用第一个值对列表列表进行排序,如果两者相等,则检查第二个,依此类推 - How to sort a list of list with the first value, if the both are equal check the second and so on 元组列表列表,按第一个元素分组并添加第二个元素 - List of lists of tuples, group by first element and add second elements 只要列表中的项目相等,按现有顺序将列表的每个元素与第二个列表的元素进行比较 - Compare each element of a list in existing order with elements of a second list in order as long as items in lists are equal 按值对 Python 字典进行排序; value 是一个包含两项的列表; 对第一个列表项进行常规排序,对第二项进行反向排序 - Sorting Python dictionary by value; value is a two-item list; regular sort on first list item, reverse sort on second item 列表元素一个列表第一个元素到第二个列表最后一个元素的总和 - list elements sum of one list first element to last element of second list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM