Reverse, sort, and sorted

Source: Internet
Author: User
Tags what parameter python list
This article describes the reverse, sort, and sorted operations in the list sorting method in Python, as well as their direct differences. For more information, see. There are three list sorting methods in python: reverse/reverse sorting by reverse, forward sorting by sort, 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 python beginners 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]

This article describes the most basic sorting method of the Python list. The list also has more advanced sorting methods. For example, you can add sorting conditions to the method, these will be introduced to you in the list advanced sorting method 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.