Application Examples:
1. Output a sequence according to the alphabet
2. Sorting multiple fields of a record, etc.
Common sort functions:
Sort ()
Sorted ()
Comparison:
1.sorted () wider range of applications
Sorted (iterable[, cmp[, key[, reverse]])
S.sorted ([cmp[, key[, reverse]])
Example:
>>> persons = [{' Name ': ' Jon ', ' age ': +}, {' name ': ' Alan ', ' age ': ' {'} ', ' name ': ' Bob ', ' age ': 23}]
>>> sorted (persons, Key=lambda x: (x[' name '],-x[' age '))
[{' Age ': ' "' Name ': ' Alan '}, {' Age ': $, ' name ': ' Bob '}, {' Age ': +, ' name ': ' Jon '}]
Sorted () can be used with any object that can be iterated, sort () generally acts on a list
>>> a = (1,2,4,2,3)
>>> A.sort ()
Traceback (most recent):
File "<stdin>", line 1, in <module>
Attributeerror: ' Tuple ' object has no attribute ' sort '
>>> Sorted (a)
[1, 2, 2, 3, 4]
2.sorted () Returns the sorted list, the original list unchanged, and sort () modifies the original list directly.
Sort () because there is no need to copy the original list, consume less memory, high efficiency
>>> a=[' 1 ', 1, ' A ', 3,7, ' n ']
>>> Sorted (a)
[1, 3, 7, ' 1 ', ' a ', ' n ']
>>> A
[' 1 ', 1, ' A ', 3, 7, ' n ']
>>> A.sort ()
>>> A
[1, 3, 7, ' 1 ', ' a ', ' n ']
3. For the sort () and sorted () functions, the incoming parameter key is more efficient than the parametric CMP. The function that the CMP passes in is called multiple times throughout the ordering process, and the key is processed only once for each element.
>>> from Timeit import Timer
>>> Timer (stmt= "sorted (Xs,key=lambda x:x[1])", setup= "xs=range (+); Xs=zip (XS,XS);"). Timeit (10000)
0.35391712188720703
>>> Timer (stmt= "Sorted (Xs,cmp=lambda a,b:cmp (a[1],b[1))", setup= "xs=range (+); Xs=zip (XS,XS);"). Timeit (10000)
0.4931659698486328
4.sorted () can sort multiple data structures
Dictionary:
Sort phonebook phone numbers by numeric size
>>> phonebook = {' Linda ': ' 7750 ', ' Bob ': ' 9345 ', ' Carol ': ' 5834 '}
>>> from operator Import Itemgetter
>>> SORTED_PB = sorted (Phonebook.iteritems (), Key=itemgetter (1))
>>> SORTED_PB
[(' Carol ', ' 5834 '), (' Linda ', ' 7750 '), (' Bob ', ' 9345 ')]
Multidimensional list:
Multi-field sorting of grades and ranks
>>> from operator Import Itemgetter
>>> Gameresult = [[' Bob ', 95.00, ' A '],[' Alan ', 86.0, ' C '],[' Mandy ', 82.5, ' A '],[' Rob ', ' the ', ', ' "E ']]
>>> Sorted (Gameresult, Key=itemgetter (2, 1))
[' Mandy ', 82.5, ' a '], [' Bob ', 95.0, ' a '], [' Alan ', 86.0, ' C '], [' Rob ', ' the ', ' ' E ']]
Mixed list in dictionary:
>>> mydict = {' Li ': [' M ', 7],
... ' Zhang ': [' E ', 2],
... ' Wang ': [' P ', 3],
... ' Du ': [' C ', 2]}
>>> from operator Import Itemgetter
>>> Sorted (Mydict.iteritems (), Key=lambda (k,v): Operator.itemgetter (1) (v))
[(' Zhang ', [' E ', 2]), (' Du ', [' C ', 2]), (' Wang ', [' P ', 3]), (' Li ', [' M ', 7])]
Mixed dictionary in list:
Sort multiple key values rating and name
>>> Gameresult = [{"Name": "Bob", "wins": Ten, "Losses": 3, "rating": 75.00},
... {"Name": "David", "wins": 3, "loses": 5, "rating": 57.00}]
>>> from operator Import Itemgetter
>>> Sorted (Gameresult,key=itemgetter ("rating", "name"))
[{' Wins ': 3, ' rating ': 57.0, ' name ': ' David ', ' Loses ': 5}, {' Wins ': ', ' losses ': 3, ' name ': ' Bob ', ' rating ': 75.0}]
>>>