Python list sort reverse, sort, sorted how-to

Source: Internet
Author: User
Tags python list

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 when playing snake Nets. 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 ']

This article explains the most basic sorting methods of the Python list, as well as more advanced sorting methods, such as the ability to include sort criteria within a method, which are described in the list advanced sorting methods Article.

Play Snake net article, reprint please indicate source and source Url: http://www.iplaypython.com/jinjie/jj114.html

Python list sort reverse, sort, sorted how-to

Related Article

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.