sorted (...)
Help on built-in function sorted in module __builtin__:
Sorted (...)
Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list
Sort (...)
Help on built-in function sort:
Sort (...)
L.sort (Cmp=none, Key=none, Reverse=false)-stable sort *in place*;
CMP (x, y)-1, 0, 1
The difference between sorted and sort
1. The sorted function is built-in, and sort is the intrinsic function of the sequence, so they are called differently, and the sorted function has a series of iterator parameters
2. The sorted function does not change the parameter series, but returns the ordered copy of the sequence, while sort acts as an intrinsic to the sequence, and the sequence of calls is sorted after the call is finished.
The following results are a good illustration of these:
>>> list=[2,1]>>> x=sorted (list) >>> x[1, 2]>>> list[2, 1]>>> y=list.sort () >>> y>>> list[1, 2]
Sorted and sort parameters sorted and sort except that one is a sequence as a parameter, one is a sequence that calls the function, the other parameters are almost identical, and each of the following describes its usage and effect:
1. Default usage
Since the parameter reverse,key,cmp of the sort function provides default parameters, we can call the function without specifying these parameter values directly. But it must have a premise that the types stored in the list are comparable. Otherwise, the error "type Error:unorderable type" will pop up.
2. Reverse Parameters
When the value is true is the reverse row, the default is false positive sequence from small to large row
>>> List.sort (reverse=false) >>> list[1, 2]>>> list.sort (reverse=true) >>> list[2 , 1]
3. The key parameter key indicates the value to be used for comparison, which is primarily useful for custom data types. Here is an example to illustrate:
# Definition for a interval.class interval:def __init__ (self, s=0, e=0): Self.start = Sself.end = e# Initialize the Interv Al Listlist = []for i in Range (10,7,-1): to J in range (11,i,-1): List.append (Interval (I,J))
Here we define the data structure of interval for [s,e] and initialize it. For this problem, it is clear that we will make an error using the default parameters because we do not provide comparable functions to compare type interval. In this case, we can specify the comparison key to solve.
#Sort the Interval listlist2 = sorted (List,key=lambda x:x.start) #Output the Interval listfor x in List:print ("[%d,%d]"% (x. Start,x.end))) for X in List2:print ("[%d,%d]"% (x.start,x.end))
Here we sort based on the Interval.start as key.
But then the question came, if I not only compare interval.start, when Interval.start equal, also want to compare interval.end, how to do?
#Sort the Interval list based on interval.start and interval.endlist2 = sorted (List,key=lambda x: (x.start,x.end))
We use Ganso (interval.start,interval.end) as the key to compare, and the meta-ancestor has the default CMP function. This achieves the goal.
4. CMP parameters We can pass a custom function or use a simple lambda as a parameter to the CMP
#Sort the Interval list based on Interval.start and Interval.enddef Cmpinterval (A, B): if A.start! = B.start:return CMP (A.S Tart,b.start) return CMP (a.end,b.end) List1 = sorted (list,cmp = cmpinterval) List2 = sorted (list,cmp = Lambda x,y:cmp ( X.start,y.start))
However, it is unfortunate to find that the introduction of CMP functions in Python 3.x will cause an error:TypeError: ' CMP ' is a invalid keyword argument for this functionAt this point we need to use key to circumvent this problem. Another suggestion is that we try to use key instead of CMP to sort to improve the speed of operation.
On the sort and sorted functions in Python