The more commonly used sort in python has two functions,
First, the definition
(1) One is the sort in the list data structure
>>> Help (List.sort)
Help on Method_descriptor:
Sort (...)
L.sort (Cmp=none, Key=none, Reverse=false)-stable sort *in place*;
CMP (x, y)-1, 0, 1
The sort () method takes optional arguments for controlling the comparisons.
CMP Specifies a custom comparison function of arguments (list items) which should return a negative, zero or Positive number depending on whether the first argument are considered smaller than, equal to, or larger than the second AR Gument: cmp=lambda x, y: cmp (X.lower (), y.lower ()). The default value is None.
key Specifies a function of one argument that's used to extract a comparison key from each list element: key =str.lower. The default value is None.
Reverse is a Boolean value. If set to True, then the list elements is sorted as if each comparison were reversed.
In general, the key and reverse conversion processes is much faster than specifying an equivalent C MP function. This is because CMP are called multiple times for each list element while key and reverse touch Each element is only once. Use Functools.cmp_to_key () to convert the Old-style CMP function to a key function.
Changed in version 2.3:support for None as a equivalent to omitting CMP is added.
Changed in version 2.4:support for key and reverse is added.
(2) Built-in function sorted
Sorted (iterable[, cmp[, key[, reverse]]])
Return a new sorted list from the items in iterable.
Indicates that the built-in sorted function generates a new data table without altering the order of the original data.
The optional arguments CMP, key, and reverse has the same meaning as those for the List.sort () method (described in section Mutable Sequence Types).
CMP Specifies a custom comparison function of the arguments (iterable elements) which should return a negative, Z Ero or positive number depending on whether the first argument are considered smaller than, equal to, or larger than the SE Cond argument: cmp=lambda x, y: cmp (X.lower (), y.lower ()). The default value is None.
key Specifies a function of one argument that's used to extract a comparison key from each list element: key =str.lower. The default value is None (compare the elements directly).
Reverse is a Boolean value. If set to True, then the list elements is sorted as if each comparison were reversed.
In general, the key and reverse conversion processes is much faster than specifying an equivalent C MP function. This is because CMP are called multiple times for each list element while key and reverse touch Each element is only once. Use Functools.cmp_to_key () to convert the Old-style CMP function to a key function.
Ii. examples
Sorting Basics
>>>sorted ([5, 2, 3, 1, 4])[1, 2, 3, 4, 5]
>>> a = [5, 2, 3, 1, 4]>>> a.sort ()>>> a[1, 2, 3, 4, 5]
sorted () -but if you don ' t need the original list, it's slightly more efficient. (Although the first method is not very convenient, but usually more efficient)
>>> Sorted ({1: ' D ', 2: ' B ', 3: ' B ', 4: ' E ', 5: ' A '})[1, 2, 3, 4, 5]
Another difference is, the list.sort () method is only defined for lists. In contrast, the sorted () function accepts any iterable.
(another point is that the List.sort () method can only be used as the data structure of a list, however, sorted is available as any data structures that can be iterated)
Key Functions
Starting with Python 2.4, both List.sort () and sorted () added a key parameter to specify a func tion to is called on every list element prior to making comparisons (since 2.4, the new key parameter has been added, by making a judgment function to sort by key)
>>> sorted ("This was a test string from Andrew". Split (), Key=str.lower)[' A ', ' Andrew ', ' from ', ' is ', ' string ', ' Test ', ' this ']
>>> student_tuples = [ (' John ', ' A ', '), (' Jane ', ' B ', '), (' Dave ', ' B ', ten), " >>> Sorted (student_tuples, Key=lambda student:student[2]) # Sort by age[(' Dave ', ' B ', ten), (' Jane ', ' B ', (' John ', ' A ', 15)]
Lambda function parameter: return value
>>> class Student:def __init__ (self, name, grade, age):Self.name = NameSelf.grade = Grade Self.age = Age def __repr__ (self): return repr ((Self.name, Self.grade, self.age)) def Weighted_grade (self): return ' CBA '. Index (Self.grade)/float (self.age) >>> student_objects = [ Student (' John ', ' A ', '), Student (' Jane ', ' B ', '), Student (' Dave ', ' B ', ten), ]>> > Sorted (student_objects, Key=lambda student:student.age) # Sort by Age[(' Dave ', ' B ', ten), (' Jane ', ' B ', '), (' John ', ' A ', ')]
span>
Dictionary ordering in Python