How to use the parameters of the python sorted () function
This article mainly introduces the relevant knowledge of "how to use the parameters of the python sorted () function". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the parameters of the python sorted () function" can help you solve the problem.
Concept
1. Sort the data in the iterable object and return a new list. Specify that lambda expressions are required for key sorting.
Grammar
Sorted (iterable, key=None, reverse=False)
Parameters.
Iterable-iterable object.
Key-elements that are mainly used for comparison, with only one parameter, the specific parameter of the function is taken from the iterable object, specifying an element in the iterable object to sort.
Reverse-collation, reverse = True descending, reverse = False ascending (default).
2. Specify the reverse order through the parameter reverse=True, and specify the field to be used in sorting by the parameter key.
(1) specify the reverse order through the parameter reverse=True:
> numbers = (4,5,2,8,9,1,0) > > sorted (numbers, reverse=True) [9,8,5,4,2,1,0]
(2) specify the fields to be used for sorting through the parameter key:
> codes = [('Shanghai', '021'), ('Beijing', '010'), ('Chengdu', '028'), ('Guangzhou', '020')] > sorted (codes, key=lambda x: X [1]) [('Beijing', '010'), ('Guangzhou', '020'), ('Shanghai', '021'), ('Chengdu') '028')] this is the end of the introduction to "how to use the parameters of the python sorted () function". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.