Python list sorting
Simply take a look at the use of the sort method of list in Python (or the sorted built-in function).
Key words:
Python list sort python dictionary sort sorted
The elements of a list can be a variety of things, strings, dictionaries, classes of their own definition, and so on.
The sorted function uses the following:
Python code
- Sorted (data, cmp=none, key=None, reverse=False)
Where data is to be sorted, you can make list or iterator, CMP, and key are functions, and the two functions work with the elements of data to produce a result, and the sorted method is sorted according to this result.
CMP (E1, E2) is a comparison function with two parameters, the return value: Negative Number: E1 < e2, 0:e1 = = e2, positive: E1 > E2. The default is None, which is the built-in comparison function.
Key is a function with one parameter, which is used to extract the comparison value for each element. The default is None, which is to directly compare each element.
In general, key and reverse are much faster than CMP because they are processed only once for each element; And CMP handles it many times.
Use examples to illustrate the usage of sorted:
1. Sorting a list of tuple components
Python code
- >>> students = [('John ', ' A ', ' ), ('Jane ', ' B ', ' ), (' Dave ', ' B ', 10),]
Sort using the key function (for lambda use see note 1)
Python code
- >>> sorted (students, key=Lambda student:student[2]) # sort by age
- [('Dave ', ' B ', ten), (' Jane ', ' B ', ') , (' John ', ' A ', ')]
Sort by CMP function
Python code
- >>> sorted (students, cmp=Lambda x,y:cmp (x[2], y[2]) # sort by age
- [('Dave ', ' B ', ten), (' Jane ', ' B ', ') , (' John ', ' A ', ')]
Use the operator function to speed up the order, which is equivalent to: (see note 2 for itemgetter usage)
Python code
- >>> from operator import itemgetter, Attrgetter
- >>> sorted (students, Key=itemgetter (2))
Multilevel sorting with operator function
Python code
- >>> sorted (students, Key=itemgetter (1,2)) # Sort by grade
- [('John ', ' A ', '), (' Dave ', ' B ', ten), (' Jane ', ' B ', ' )]
2. Sort by dictionary
Python code
- >>> d = { ' data1 ': 3, ' data2 ': 1, 2, ' data4 ': 4}&NBSP;&NBSP;
- >>> sorted (D.iteritems (), key= Itemgetter (1), reverse=true)
- [( ' data4 ', 4), ( ' data1 ', < Span class= "number" >3), ( ' data3 ', 2), ( ' data2 ', 1)]
Note 1
Reference: http://jasonwu.me/2011/10/29/introduce-to-python-lambda.html
NOTE 2
Reference: http://ar.newsmth.net/thread-90745710c90cf1.html
Class Itemgetter (__builtin__.object)
| Itemgetter (item, ...)-Itemgetter object
|
| Return a callable object that fetches the given item (s) from its operand.
| After, F=itemgetter (2), the call F (r) returns R[2].
| After, G=itemgetter (2,5,3), the call G (r) returns (R[2], r[5], r[3])
Equivalent
Python code
- def itemgetter (i,*a):
- def func (obj):
- r = Obj[i]
- If a:
- R = (r,) + tuple (Obj[i] for i in a)
- return R
- return func
- >>> a = [1,2,3]
- >>> B=operator.itemgetter (1)
- >>> B (a)
- 2
- >>> B=operator.itemgetter (1,0)
- >>> B (a)
- (2, 1)
- >>> B=itemgetter (1)
- >>> B (a)
- 2
- >>> B=itemgetter (1,0)
- >>> B (a)
- (2, 1)
Resources:
1. http://www.linuxso.com/linuxbiancheng/13340.html
2. http://www.douban.com/note/13460891/
The use of the sort method of the list in Python