Python List Sorting method

Source: Internet
Author: User
There are three list sorting methods in the Python language: Reverse reverse/reverse order, sort positive order, and sorted to get a sorted list. In the more advanced list sort, the latter two methods can also be sorted by adding conditional parameters.
Reverse () method

Reverses the order of 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 reversal Sort: The order of the elements in the original list is re-deposited from left to right without sorting the parameters in the list. If you need to organize the parameters in the list, you need to sort the list in a different sort order.
Sort () Sorting method

This function method sorts the contents of the list forward, and the sorted new list overwrites the original list (the ID remains the same), that is, the sort method is to directly modify the list ordering 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 you need a sorted list, and then you want to save the original unordered lists, and they do the following:
>>> a = [5,7,6,3,4,1,2]
>>> B = A.sort ()
>>> Print B
None

At this point the problem arises, and variable B gets a null value. What if you want to get a sorted list and want to keep the original list? The list sorted () method can be implemented for you.
Sorted () method

That is, you can keep the original list, and you can get the sorted list sorted () operation method 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 is always returned in a list form:
>>> sorted (' iplaypython.com ')
['. ', ' A ', ' C ', ' h ', ' I ', ' l ', ' m ', ' n ', ' o ', ' o ', ' P ', ' P ', ' t ', ' y ', ' y ']
The difference between three people

Sort () is a method of mutable objects (dictionaries, lists), no arguments, no return values, and sort () alters mutable objects, so no return value is required. The sort () method is a method or property unique to a Mutable object, and as immutable objects such as tuples, strings do not have these methods, and if called will return an exception.
>>> a=[5,4,3,2,1]
>>> A.sort ()
>>>
>>> A
[1, 2, 3, 4, 5]


Sorted () is a python built-in function, not a unique method of a Mutable object (list, dictionary), and the sorted () function requires a parameter (the argument can be a list, a dictionary, a tuple, a string), and whatever parameter is passed, returns a return value in the list as a container. If it is, the dictionary returns 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 the same as sort, and 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 "reversal" effect 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]
  • Contact Us

    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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.