Welcome to stationmaster Online webmaster school learning Python Knowledge, this paper studies in Python Detailed explanation of sorting the list in. The main contents of this knowledge point include: sorting using the sort() method of list objects, sorting using the built-in sorted() function, and reverse() method.
In the actual development of Python, it is often necessary to sort the list. below Webmaster Online Explain several common methods for sorting lists:
1. Sort using the sort() method of the list object.
The list object provides a sort () method to sort the elements in the original list. After sorting, the order of elements in the original list will change. The syntax format of sort() method of list object is as follows:
listname.sort(key=None,reverse=False)
Relevant parameters are described as follows:
listname: indicates the list to be sorted.
Key: specifies a comparison key to extract from each list element. (for example, setting "key=str.lower" means that the sorting is not case sensitive).
reverse: optional parameter. If its value is specified as True, it indicates descending sorting; If it is specified as False, it indicates ascending order. The default is ascending.
For example, the math scores of 10 male students in class 1, grade 3 of a middle school are listed, and then sorted by sort() method, code As follows:
s = [93,95,86,98,99,99,89,100,100,97] print("Original list:",s) s.sort() print("Ascending order:",s) s.sort(reverse=True) print("Descending order:",s)
The results are as follows:
Original list: [93, 95, 86, 98, 99, 99, 89, 100, 100, 97] Ascending order: [86, 89, 93, 95, 97, 98, 99, 99, 100, 100] Descending order: [100, 100, 99, 99, 98, 97, 95, 93, 89, 86] >>>
Using the sort() method, you can sort not only numeric values, but also multiple strings. Sorting strings is case sensitive by default. If you want to be case insensitive, you need to specify its key parameter.
For example, the column definition saves a list of English strings with different uppercase and lowercase, and then sorts them with the sort() method. The code is as follows:
s = ['hello','polly','Lucy','Lily','Han Meimei'] s.sort() print("Case sensitive:",s) s.sort(key=str.lower) print("Case insensitive:",s)
The operation results are as follows:
Case sensitive: ['Han Meimei', 'Lily', 'Lucy', 'hello', 'polly'] Case insensitive: ['Han Meimei', 'hello', 'Lily', 'Lucy', 'polly'] >>>
Note: the sort() method does not support the most Chinese when sorting the list. The sorting result is inconsistent with our common sorting by pinyin or stroke. To sort Chinese content, you need to rewrite the corresponding method instead of using the sort() method directly. For example:
s = ['Zhang San','Li Si','Wang Wu','Li Ming','Jun Yang'] s.sort() print(s)
Operation results:
['Zhang San', 'Li Si', 'Li Ming', 'Jun Yang', 'Wang Wu'] >>>
We can't understand this result at all, so we can't directly use the sort() method to sort the Chinese list.
2. Sort using the built-in sorted() function.
In Python, a built-in sorted () function is provided to sort the list. After sorting with this function, the element order of the original list remains unchanged. The syntax format of the sorted() function is as follows:
sorted(iterable,key=None,reverse=False)
Relevant parameters are described as follows:
iterable: indicates the list to be sorted.
Key: specifies to extract a comparison key from each list element. (for example, setting "key=str.lower" means that the sorting is not case sensitive).
reverse: optional parameter. If its value is specified as True, it indicates descending sorting; If it is specified as False, it indicates ascending order. The default is ascending.
For example, the math scores of 10 male students in class 1, grade 3 of a middle school are listed, and then sorted by the sorted() function. The code is as follows:
s = [93,95,86,98,99,99,89,100,100,97] s1 = sorted(s) print("Ascending order:",s1) s2 = sorted(s,reverse=True) print("Descending order:",s2) print("Original list:",s)
The operation results are as follows:
Ascending order: [86, 89, 93, 95, 97, 98, 99, 99, 100, 100] Descending order: [100, 100, 99, 99, 98, 97, 95, 93, 89, 86] Original list: [93, 95, 86, 98, 99, 99, 89, 100, 100, 97] >>>
explain:
The sort() method and the sorted() function of a list object do the same thing. There are two different points:
a. The sort() method can only handle the sorting of list type data; The sorted() function can handle sorting of multiple types of data.
b. The sort() method will modify the sorting of the elements of the original list; The sorted() function does not modify the original data, but creates a copy of the original list, but returns a sorted list.
3. Reverse the sort using the reverse() method.
When we use the sort() method and the sorted() function, we use reverse when dealing with ascending and descending orders.
Reverse means reverse. In fact, there is a special sort, reverse sort. Relevant codes are as follows:
s = [93,95,86,98,99,99,89,100,100,97] python = ["grace","to make clear","simple"] s.reverse() python.reverse() print(s) print(python)
The operation results are as follows:
[97, 100, 100, 89, 99, 99, 98, 86, 95, 93] ['simple', 'to make clear', 'grace'] >>>
In the actual Python development, the reverse() method is used for reverse sorting, which is not mentioned in many tutorials. Because less is used.
So far, in sorting lists in python, which is learned in this article, you can use the sort() method of the list object to sort, the built-in sorted() function to sort, and the reverse() method to reverse sort. It's all explained. If you don't understand, you can leave a message to me!