Sorting instances of lists in Python

  • 2020-04-02 14:30:12
  • OfStack

Many times we need to sort a List, and Python provides two ways to sort a given List L:

Method 1. Use the member function sort of List to sort
Method 2. Sorted with the built-in function sorted (starting at 2.4)

These two methods are similar in use. Take the first one as an example:

Since Python2.4, the sort method has three optional arguments, as described in Python Library Reference


cmp:cmp specifies a custom comparison function of two arguments (iterable elements) which should return a negative, zero or positive number depending on whether the first argument is considered smaller than, equal to, or larger than the second argument:
"cmp=lambda x,y: cmp(x.lower(), y.lower())"
key:key specifies a function of one argument that is used to extract a comparison key from each list element: "key=str.lower"
reverse:reverse is a boolean value. If set to True, then the list elements are sorted as if each comparison were reversed.In general, the key and reverse conversion processes are much faster than specifying an
equivalent cmp function. This is because cmp is called multiple times for each list element while key and reverse touch each element only once.

Here is a specific example of sort.
Example 1:

>>>L = [2,3,1,4]
>>>L.sort()
>>>L
>>>[1,2,3,4]

Example 2:

>>>L = [2,3,1,4]
>>>L.sort(reverse=True)
>>>L
>>>[4,3,2,1]

Example 3:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(cmp=lambda x,y:cmp(x[1],y[1]))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Example 4:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>L.sort(key=lambda x:x[1])
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Example 5:

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>import operator
>>>L.sort(key=operator.itemgetter(1))
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

Example 6: (DSU method: Decorate - Sort - Undercorate)

>>>L = [('b',2),('a',1),('c',3),('d',4)]
>>>A = [(x[1],i,x) for i,x in enumerate(L)] #i can confirm the stable sort
>>>A.sort()
>>>L = [s[2] for s in A]
>>>L
>>>[('a', 1), ('b', 2), ('c', 3), ('d', 4)]

The method of sorting the List in 6 is given above, in which example 3.4.5.6 can pair an item in the List item
Sort the comparison keywords.
Efficiency comparison:

cmp < DSU < key

Through experimental comparison, method 3 is slower than method 6, method 6 is slower than method 4, and method 4 is basically the same as method 5
Multi-keyword comparison sort:
Example 7:

>>>L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:x[1])
>>> L
>>>[('d', 2), ('c', 2), ('b', 3), ('a', 4)]

We see that the sorted L is only sorted by the second keyword, if we want to use the second keyword
Sort it by the first keyword after it's sorted ? There are two ways
Example 8:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=lambda x:(x[1],x[0]))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

Example 9:

>>> L = [('d',2),('a',4),('b',3),('c',2)]
>>> L.sort(key=operator.itemgetter(1,0))
>>> L
>>>[('c', 2), ('d', 2), ('b', 3), ('a', 4)]

Why does instance 8 work? The reason is that a tuple compares one from left to right, compares the first one, if
Equal. Compare the second one


Related articles: