Python sort and sorted usage notes

Source: Internet
Author: User
Tags python list

Python sort and sorted usage notes

Python list built-in sort () method for sorting, you can also use the python built-in Global sorted () method to generate a new sequence for the iterative sequence sorting

I. Simplest sorting

1. sort by sort

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

Using the sort () method to sort the list changes the list itself and does not return a new list. This method is generally not as convenient as sorted (), but if you do not need to keep the original list, this method will be more effective sort ().

Sort () cannot sort dict dictionaries

2. sort by sorted ()

My_list = [3, 5, 1, 4, 2] result = 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 # output: ['A', 'B', 'C']

By default, dict is sorted by the key value of dict. The returned result is a list sorted by the key value.

II. key Parameters

Starting from python2.4, the list. sort () and sorted () functions add the key parameter to specify a function, which will be called before each element comparison

The value of the key parameter is a function. This function has only one parameter and returns a value for comparison. This technology is quick because the function specified by the key will call each element accurately.

1. Sort complex tuples

Student_tuples = [('john', 'A', 15), ('Jane ',' B ', 12), ('Dave', 'B', 10),] result = sorted (student_tuples, key = lambda student: student [2]) print result # output [('Dave ',' B ', 10), ('Jane ', 'B', 12), ('john', 'A', 15)]

We can see that the sorting is based on the 10, 12, and 15 values, because the values returned by function lambda student: student [2] are 10, 12, and 15 respectively.

Therefore, the values returned by the function are compared. Keys = 15, keys = 12, and keys = 10 are compared based on the returned values;

Lambda student: student [2] is equivalent

Def f (student ):

Return student [2]

2. sort by dictionary value

By default, sorted sorts the keys of dict. If you want to sort the values of dict, You need to specify the key parameter.

My_dict = {"a": "2", "c": "5", "B": "1"} result = sorted (my_dict) print result # dict is sorted by default. If the key parameter is not specified, the key values of dict are compared and sorted by default. # result output: ['A', 'B ', 'C'] result2 = sorted (my_dict, key = lambda x: my_dict [x]) print result2 # specify the key parameter and sort it by the value of dict # result2 output: ['B', 'A', 'C']

The reverse parameter of sorted () must be set to False or True to indicate whether the request is in reverse order.

Sorted () and other parameters such as cmp parameters will not be described here.

Here, I want to record the notes I need. For more usage, Google.

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.