The sort of method in Python USES the basic tutorial

  • 2020-05-19 05:08:40
  • OfStack

1. Basic form


sorted(iterable[, cmp[, key[, reverse]]])
iterable.sort(cmp[, key[, reverse]])

Parameter explanation:

(1) iterable specifies list or iterable to be sorted, needless to say;

(2) cmp is a function, which can be used to specify one function or lambda function for comparison when sorting, such as:

students is the list of class object, and every member has three fields. When comparing with sorted, you can determine the cmp function by yourself. For example, here you need to sort by comparing the third data member, the code can be written as follows:


students = [('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10)]
sorted(students, key=lambda student : student[2])

(3) key is a function that specifies which item of the element to be sorted is to be sorted. The function is illustrated by the above example. The code is as follows:


sorted(students, key=lambda student : student[2])

The lambda function specified by key is to go to the third field of the element student (that is, student[2]), so when sorted sorts, it sorts by the third field of all the elements of students.

2. Common usage:

1. Address sorting

1) the list has its own sort method, which sorts the list by its original address. Since it is sorted by its original address, it is obvious that tuples cannot have this method, because tuples are unmodifiable.


x = [4, 6, 2, 1, 7, 9]
x.sort()
print x # [1, 2, 4, 6, 7, 9]

2. Copy sorting

1) [:] shard 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[:] all elements of x are copied to y by sharding. If you simply assign x to y: y = x, y and x still 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']

3. Advanced usage

1. Custom cmp comparison function


def comp(x, y):
if x < y:
return 1
elif x > y:
return -1
else:
return 0
nums = [3, 2, 8 ,0 , 1]
nums.sort(comp)
print nums #  Descending order [8, 3, 2, 1, 0]
nums.sort(cmp) #  Call the built-in function cmp  , sort in ascending order 
print nums #  Descending order [0, 1, 2, 3, 8]

2. Customize key and reverse

1.reverse is in descending order, so it needs to provide a Boolean value, which is False (ascending order) by default.

2. When key is used, it must provide a function that is always called by the sorting process:


alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
#  Sort at multiple levels, first by order 3 Order the elements, and then by number 2 Sorting of elements: 
print sorted(alist, cmp = None, key = lambda x:(int(x[2]), int(x[1])), reverse = False)
-------------------------------------------------------------------------------------------
[('1', '2', '3'), ('5', '6', '7'), ('2', '3', '10'), ('2', '4', '10'), ('2', '5', '10')]

4. operator. itemgetter function

The itemgetter function provided by the operator module is used to obtain the data of which dimensions of the object. The parameter is 1 sequence number (that is, the sequence number of the data to be acquired in the object). Here is an example.


a = [1,2,3]
>>> b=operator.itemgetter(1)   // Define a function b , gets the value of the object 1 The value of a domain 
>>> b(a)
2
>>> b=operator.itemgetter(1,0) // Define a function b , gets the value of the object 1 A domain and the first 0 The value of a 
>>> b(a)
(2, 1)

Note that instead of getting a value, the operator.itemgetter function defines a function that is applied to an object to get a value.

Usage of itemgetter in sort:


from operator import itemgetter
alist = [('2', '3', '10'), ('1', '2', '3'), ('5', '6', '7'), ('2', '5', '10'), ('2', '4', '10')]
#  Sort at multiple levels, first by order 3 Order the elements, and then by number 2 Sorting of elements: 
print sorted(alist, cmp = None, key = itemgetter(2, 1), reverse = False)
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')]

The above is the basis of sort() method in Python introduced to you by this site. I hope it can be of help to you. If you have any questions, please feel free to leave a message to me.


Related articles: