Empirically, it seems that Python's default list sorter, when passed a list of tuples, will sort by the first element in each tuple. Is that correct? If not, what's the right way to sort a list of tuples by their first elements?
从经验上看,Python的默认列表排序器在传入元组列表时,似乎会按每个元组中的第一个元素进行排序。那是正确的吗?如果不是,按元组的第一个元素排序元组的正确方法是什么?
5 个解决方案
#1
69
It automatically sorts a list of tuples by the first elements in the tuples, then by the second elements and so on tuple([1,2,3]) will go before tuple([1,2,4]). If you want to override this behaviour pass a callable as the second argument to the sort method. This callable should return 1, -1, 0.
它会根据元组中的第一个元素自动对元组列表进行排序,然后再根据元组中的第二个元素([1,2,3])对元组进行排序,直到元组([1,2,4])。如果要重写此行为,请将可调用性作为第二个参数传递给sort方法。这个可调用函数应该返回1 - 1,0。
#2
8
Yes, this is the default. In fact, this is the basis of the classic "DSU" (Decorate-Sort-Undecorate) idiom in Python. See Code Like a Pythonista.
是的,这是默认值。事实上,这是Python中经典的“DSU”(修饰-排序-取消修饰)习语的基础。类似于python的代码。
#3
5
No, tuples are sequence types just like strings. They are sorted the same, by comparing each element in turn:
不,元组是序列类型,就像字符串一样。它们的排序相同,依次比较每个元素:
>>> import random
>>> sorted([(0,0,0,int(random.getrandbits(4))) for x in xrange(10)])
[(0, 0, 0, 0), (0, 0, 0, 4), (0, 0, 0, 5), (0, 0, 0, 7), (0, 0, 0, 8),
(0, 0, 0, 9), (0, 0, 0, 12), (0, 0, 0, 12), (0, 0, 0, 12), (0, 0, 0, 14)]
The three zeroes are only there to show that something other than the first element must be getting inspected.
三个0只表示必须检查除第一个元素之外的其他元素。
#4
0
Try using the internal list sort method and pass a lambda. If your tuples first element is a integer, this should work.
尝试使用内部列表排序方法并传递一个lambda。如果您的元组第一个元素是一个整数,那么它应该可以工作。
# l is the list of tuples
l.sort(lambda x,y: x-y)
You can use any callable for the compare function, not necessarily a lambda. However it needs to return -1 (less than), 0 (equal) or 1 (greater than).
您可以对compare函数使用任何可调用的值,而不一定是lambda。但是它需要返回-1(小于)、0(等于)或1(大于)。
#5
0
Check out "Devin Jeanpierre" answer to this question sort-a-dictionary-in-python-by-the-value where he says to use a tuple and shows how to sort by the second value
请查看“Devin Jeanpierre”对这个问题的回答。在这个问题中,or -a-dictionary-in-python-by- value表示要使用tuple,并显示如何按第二个值排序
#1
69
It automatically sorts a list of tuples by the first elements in the tuples, then by the second elements and so on tuple([1,2,3]) will go before tuple([1,2,4]). If you want to override this behaviour pass a callable as the second argument to the sort method. This callable should return 1, -1, 0.
它会根据元组中的第一个元素自动对元组列表进行排序,然后再根据元组中的第二个元素([1,2,3])对元组进行排序,直到元组([1,2,4])。如果要重写此行为,请将可调用性作为第二个参数传递给sort方法。这个可调用函数应该返回1 - 1,0。
#2
8
Yes, this is the default. In fact, this is the basis of the classic "DSU" (Decorate-Sort-Undecorate) idiom in Python. See Code Like a Pythonista.
是的,这是默认值。事实上,这是Python中经典的“DSU”(修饰-排序-取消修饰)习语的基础。类似于python的代码。
#3
5
No, tuples are sequence types just like strings. They are sorted the same, by comparing each element in turn:
不,元组是序列类型,就像字符串一样。它们的排序相同,依次比较每个元素:
>>> import random
>>> sorted([(0,0,0,int(random.getrandbits(4))) for x in xrange(10)])
[(0, 0, 0, 0), (0, 0, 0, 4), (0, 0, 0, 5), (0, 0, 0, 7), (0, 0, 0, 8),
(0, 0, 0, 9), (0, 0, 0, 12), (0, 0, 0, 12), (0, 0, 0, 12), (0, 0, 0, 14)]
The three zeroes are only there to show that something other than the first element must be getting inspected.
三个0只表示必须检查除第一个元素之外的其他元素。
#4
0
Try using the internal list sort method and pass a lambda. If your tuples first element is a integer, this should work.
尝试使用内部列表排序方法并传递一个lambda。如果您的元组第一个元素是一个整数,那么它应该可以工作。
# l is the list of tuples
l.sort(lambda x,y: x-y)
You can use any callable for the compare function, not necessarily a lambda. However it needs to return -1 (less than), 0 (equal) or 1 (greater than).
您可以对compare函数使用任何可调用的值,而不一定是lambda。但是它需要返回-1(小于)、0(等于)或1(大于)。
#5
0
Check out "Devin Jeanpierre" answer to this question sort-a-dictionary-in-python-by-the-value where he says to use a tuple and shows how to sort by the second value
请查看“Devin Jeanpierre”对这个问题的回答。在这个问题中,or -a-dictionary-in-python-by- value表示要使用tuple,并显示如何按第二个值排序