This article and everyone to share is mainly python in the sort () method related knowledge, come together to see it, hope to learn Python help you.
First, the basic form
Sorted (iterable[, cmp[, key[, reverse]])
Iterable. Sort (cmp[, key[, reverse])
Parameter explanation:
(1) iterable specifies the list or iterable to sort, not to mention;
(2) CMP is a function that specifies a function to compare when sorting, you can specify a function or a lambda function, such as:
Students is a list of class objects, no member has three fields, you can set the CMP function by comparing it with sorted, for example, to sort by comparing the third data member, the code can write:
Students = [(' John ', ' A ', '), (' Jane ', ' B ', '), (' Dave ', ' B ', 10)]
Sorted (students, KEY=LAMBDA student:student[2])
(3) key is a function that specifies which item to sort the elements to be sorted, the function is illustrated by the above example, the code is as follows:
Sorted (Students, KEY=LAMBDA student : student[2])
The function of the lambda function specified by key is to go to the third field of the element student (i.e.: student[2]), so when sorted is sorted, it is sorted by the third field that students all the elements.
Second, general usage:
1. On-site sorting
1) The list has its own sort method, which lists the list in the original order, since it is the original sort, it is obvious that tuples can not have this method, because the tuple is not modifiable.
x = [4, 6, 2, 1, 7, 9]
X.sort ()
Print x # [1, 2, 4, 6, 7, 9]
2. Sorting replicas
1) [:] Sharding method
X =[4, 6, 2, 1, 7, 9]
y = x[:]
Y.sort ()
Print y #[1, 2, 4, 6, 7, 9]
Print x #[4, 6, 2, 1, 7, 9]
Note: y = x[:] Copies all the elements of the list x to Y through a shard operation, if you simply assign X to Y:y = x, y and X, or point to the same list, and no new copy is generated.
2) Sorted method
Sorted returns an ordered copy, and the type is always a list, as follows:
X =[4, 6, 2, 1, 7, 9]
y = sorted (x)
Print y #[1, 2, 4, 6, 7, 9]
Print x #[4, 6, 2, 1, 7, 9]
Print sorted (' Python ') #[' P ', ' h ', ' n ', ' o ', ' t ', ' Y ']
Iii. Advanced Usage
1. Custom CMP comparison functions
def Comp (x, y):if x < y:return 1elif x > Y:return -1else:return 0
Nums = [3, 2, 8, 0, 1]
Nums.sort (comp)print nums # descending sort [8, 3, 2, 1, 0]
Nums.sort (CMP) # Call the built-in function cmp, sort in ascending order print nums # descending sort [0, 1, 2, 3, 8]
2. customizing keys and Reverse
1. Reverse implements a descending sort, which needs to provide a Boolean value that defaults to False (ascending order).
2.key must provide a function for the total call of the sort procedure when used:
x = [' mmm ', ' mm ', ' mm ', ' m ']x. Sort (key = Len) Print x # [' m ', ' mm ', ' mm ', ' mmm ']
Alist = [(' 2 ', ' 3 ', ' 10 '), (' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 '), (' 2 ', ' 5 ', ' 10 '), (' 2 ', ' 4 ', ' 10 ')]# multilevel sort, first sorted by 3rd element, and then followed by the 2nd dollar Vegetarian Sort:print sorted (alist, CMP = None, key = Lambda xint (x[2]), int (x[1]), reverse = false)
-------------------------------------------------------------------------------------------
[(' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 '), (' 2 ', ' 3 ', ' 10 '), (' 2 ', ' 4 ', ' 10 '), (' 2 ', ' 5 ', ' 10 ')]
Four, Operator.itemgetter function
The Itemgetter function provided by the operator module is used to get the data of which dimensions of the object, with some ordinal numbers (that is, the number of the data that needs to be fetched in the object), as shown in the example below.
A = [A]
>>> b=operator. Itemgetter (1)//define function B, get the value of the 1th field of an object
>>> B (a) 2
>>> b=operator. Itemgetter (1,0)//define function B, get the 1th field and No. 0 value of an object
>>> B (a)
(2, 1)
Note that the Operator.itemgetter function does not get a value, but instead defines a function that acts on the object to get the value.
Itemgetter usage in sort:
from operator import Itemgetter
Alist = [(' 2 ', ' 3 ', ' 10 '), (' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 '), (' 2 ', ' 5 ', ' 10 '), (' 2 ', ' 4 ', ' 10 ')]
# Multilevel sorting, sorted by the 3rd element first, and then sorted by the 2nd element: print sorted (alist, CMP = none , key = Itemgetter (2, 1), reverse = print sorted (alist, CMP = none , key = lambda x:itemgetter (2, 1) (x), reverse = false ) print sorted (alist, CMP = none , key = LAMBDA x:map (int, Itemgetter (2, 1) (x)), reverse = false )
--------------------------------------------------------------------------------------------------
[(' 2 ', ' 3 ', ' 10 '), (' 2 ', ' 4 ', ' 10 '), (' 2 ', ' 5 ', ' 10 '), (' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 ')]
[(' 2 ', ' 3 ', ' 10 '), (' 2 ', ' 4 ', ' 10 '), (' 2 ', ' 5 ', ' 10 '), (' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 ')]
[(' 1 ', ' 2 ', ' 3 '), (' 5 ', ' 6 ', ' 7 '), (' 2 ', ' 3 ', ' 10 '), (' 2 ', ' 4 ', ' 10 '), (' 2 ', ' 5 ', ' 10 ')]
Source: Blog Park
Use of the sort () method in Python