There are three list sorting methods in python: reverse sorting by reverse, forward sorting by sort, and sorted. In more advanced list sorting, there are also three list sorting methods in the last two methods in python: reverse/reverse sorting, sort forward sorting, and sorted. 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()>>> a[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 bNone
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(a)>>> a[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()>>> >>> a[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]'12345'>>> mytuple[::-1](1, 2, 3, 4, 5)>>> mylist[::-1][1, 2, 3, 4, 5]
The above is a detailed description of the Python list sorting method reverse, sort, sorted. For more information, see other related articles in the first PHP community!