Python list sorting methods reverse, sort, and sorted are described in detail in the python language. There are three list sorting methods: reverse/reverse sorting, sort forward sorting, and sorted to obtain the list after sorting. In more advanced list sorting, you can add condition parameters to the last two methods for sorting.
Reverse () method
Sorts the elements in the list in reverse order, as shown below:
>>> X = [1, 5, 2, 3, 4]
>>> X. reverse ()
>>> X
[4, 3, 2, 5, 1]
Reverse sorting of the reverse list: stores the elements in the original list from left to right without sorting the parameters in the list. If you need to sort the parameters in the list, you need to use another sort method of the list, sort, to sort the parameters in the forward order.
Sort () sorting method
This function is used to sort the list content in a forward manner. The new list after sorting will overwrite the original list (id unchanged), that is, the sort sorting method directly modifies the sorting method of the original list.
>>> A = [5, 7, 6, 3, 4, 1, 2]
>>> A. sort ()
>>>
[1, 2, 3, 4, 5, 6, 7]
Many beginners of python are confused about the sort () method. Sometimes you need a sorted list and want to save the original unsorted list. they will do this:
>>> A = [5, 7, 6, 3, 4, 1, 2]
>>> B = a. sort ()
>>> Print B
None
At this time, the problem occurs. variable B gets a null value. What if I want to get a sorted list and keep the original list? The list sorted () method can be implemented for you.
Sorted () method
You can retain the original list and get the sorted list sorted () as follows:
>>> A = [5, 7, 6, 3, 4, 1, 2]
>>> B = sorted ()
>>>
[5, 7, 6, 3, 4, 1, 2]
>>> B
[1, 2, 3, 4, 5, 6, 7]
The sorted () method can be used in a sequence of any data type. the returned result is always in the form of a list:
>>> Sorted ('iplaypython. com ')
['. ', 'A', 'C', 'H',' I ', 'L', 'M', 'n', 'O', 'O ', 'P', 'P', 'T', 'y', 'y']
Differences between the three
Sort () is a variable object (dictionary, list) method, no parameter, no return value, sort () will change the variable object, so no return value. The sort () method is a method or attribute exclusive to a mutable object. as an immutable object, such as tuples and strings, it does not have these methods. if it is called, an exception is returned.
>>> A = [5, 4, 3, 2, 1]
>>> A. sort ()
>>>
>>>
[1, 2, 3, 4, 5]
Sorted () is a built-in function of python and is not a special method of variable objects (lists and dictionaries). sorted () A function requires a parameter (the parameter can be a list, dictionary, tuples, or string). No matter what parameter is passed, a return value using the list as the container is returned, if it is a dictionary, the list of return keys is displayed.
>>> Mystring = "54321"
>>> Mytuple = (5, 4, 3, 2, 1)
>>> Mylist = [5, 4, 3, 2, 1]
>>> Sorted (mystring)
['1', '2', '3', '4', '5']
>>> Sorted (mytuple)
[1, 2, 3, 4, 5]
>>> Sorted (mylist)
[1, 2, 3, 4, 5]
Reverse () and sort are used in the same way, while reversed () and sorted () are used in the same way.
>>> Mylist = [5, 4, 3, 2, 1]
>>> Mylist. reverse ()
>>> Mylist
[1, 2, 3, 4, 5]
>>> Mylist = [5, 4, 3, 2, 1]
>>> For I in reversed (mylist ):
... Print I,
...
1 2 3 4 5
Through the slice of the sequence, the effect of "reversal" can also be achieved.
>>> Mystring = "54321"
>>> Mytuple = (5, 4, 3, 2, 1)
>>> Mylist = [5, 4, 3, 2, 1]
>>> Mystring [:-1]
'123'
>>> Mytuple [:-1]
(1, 2, 3, 4, 5)
>>> Mylist [:-1]
[1, 2, 3, 4, 5]
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service