Detailed Explanation of Python List Sorting Methods reverse sort and sorted

  • 2021-10-24 23:14:18
  • OfStack

There are three list sorting methods in python language:

reverse Reverse/Reverse Sort

sort positive order sorting

sorted can get the sorted list

In higher-level list sorting, the last two methods can also add conditional parameters for sorting.

reverse () method

Reverse and sort the elements in the list, such as the following


>>> x = [1,5,2,3,4]
>>> x.reverse()
>>> x
[4, 3, 2, 5, 1]

reverse List Reverse Sort: Is the original list of elements from left to right order to re-store, and will not sort the parameters in the list. If you need to sort out the parameters in the list, you need to use another sorting method of the list, sort positive sorting.

sort () sorting method

This function method sorts the contents of the list forward, and the sorted new list will overwrite the original list (id remains unchanged), that is, the sort sorting method is to directly modify the original list list sorting method.


>>> a = [5,7,6,3,4,1,2]
>>> a.sort()
>>> a
[1, 2, 3, 4, 5, 6, 7]

Many python beginners are confused about the sort () method. Sometimes they 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 point, the problem arises, and the variable b gets a null value. So what if you want to get a sorted list and keep the original list? The list sorted () method can help you implement it.

sorted () method

You can keep the original list and get the sorted list. The operation method of sorted () is 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 and always returns a list:


>>> sorted('ofstack.com')
['.', '1', '5', 'b', 'e', 'j', 'n', 't']

The difference between the three

sort () is a method of mutable objects (dictionaries, lists) with no parameters and no return value, while sort () changes mutable objects, so no return value is required. sort () methods are methods or properties unique to mutable objects, whereas strings, as immutable objects such as tuples, do not have these methods and will return 1 exception if called.


>>> 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 unique method of mutable objects (lists, dictionaries). sorted () function needs 1 parameter (parameters can be lists, dictionaries, tuples, strings). No matter what parameters are passed, it will return a return value with lists as containers. If it is dictionaries, it will return a list of keys.


>>> 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 () is used in the same way as sort, while reversed () is used in the same way as sorted ()


>>> 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

The effect of "reversal" can also be achieved by slicing the sequence


>>> 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]

This is the end of the explanation of Python list sorting methods reverse, sort and sorted. Please see the following links for more knowledge about this


Related articles: