Python sort and sorted using notes

Source: Internet
Author: User

The Python list built-in sort () method is used for sorting, or it can use the Python built-in global sorted () method to generate a new sequence for an iterative sequence ordering

One, the simplest sort of

1. Sort by using sort

My_list = [3, 5, 1, 4, 2]my_list.sort ()print  my_list# output: [1, 2, 3, 4, 5]

Sorting the list by using the sort () method modifies the list itself, does not return a new list, and is generally less convenient than sorted (), but if you do not need to keep the original list, this method will be more efficient sort ().

Sort () does not order the Dict dictionary

2. Sorting using sorted ()

My_list = [3, 5, 1, 4, 2= sorted (my_list)print  result# output: [1, 2, 3, 4, 5] 

 my_dict = { " a   ": "  1   ", "  c   ": "  3   ", "  b   ": "  2   " }result  = sorted (my_dict)  print   result  #  Span style= "COLOR: #008000" > output: [' A ', ' B ', ' C ']  

The Dict sort by default will be sorted by the key value of dict, and the result returned is a list that sorts the key values.

Two, key parameter

Starting with python2.4, the List.sort () and sorted () functions add a key parameter to specify a function that will be called before each element is compared.

the value of the key parameter is a function that has only one parameter and returns a value to be used for comparison . This technique is fast because the function specified by key will be called exactly for each element.

1. Sorting Complex tuples

Student_tuples = [        ('John','A', 15),        ('Jane','B', 12),        ('Dave','B', 10),]result= Sorted (Student_tuples, key=LambdaStudent:student[2])Printresult#output [(' Dave ', ' B ', ten), (' Jane ', ' B ', '), (' John ', ' A ', ')]

As you can see, the sort is sorted by 10, 12, 15 values, because the function lambda student:student[2] returns values of 10, 12, 15, respectively.

Therefore, the values returned by the function are compared; key=15, key=12,key=10 are compared according to these return values;

Lambda Student:student[2] is equivalent to

def f (Student):

return student[2]

2. Sort by the value of the dictionary

The default sorted is to sort dict key, and if you want to sort by the value of dict, you need to specify the key parameter.

My_dict = {"a":"2","C":"5","b":"1"}result=Sorted (my_dict)Printresult#by default, the Dict is sorted, the key parameter is not specified, and the key value of Dict is sorted by default#result output: [' A ', ' B ', ' C ']result2= Sorted (My_dict, key=LambdaX:my_dict[x])Printresult2#Specifies the key parameter, sorted according to the value of Dict#result2 output: [' B ', ' A ', ' C ']

The reverse parameter of sorted () accepts false or true to indicate whether to reverse

Sorted () Other parameters, such as CMP parameters, are not described here.

Here I record the need to note, as far as there are more usage can be self-Baidu.

Python sort and sorted using notes

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.