Python tips: Sorting list items, python list items

Source: Internet
Author: User

Python tips: Sorting list items, python list items

This article describes the sorting of Python list items for your reference. Let's take a look at the details below:

Typical code 1:

data_list = [6, 9, 1, 3, 0, 10, 100, -100] data_list.sort() print(data_list) 

Output 1:

[-100, 0, 1, 3, 6, 9, 10, 100] 

Typical Code 2:

data_list = [6, 9, 1, 3, 0, 10, 100, -100] data_list_copy = sorted(data_list) print(data_list) print(data_list_copy) 

Output 2:

[6, 9, 1, 3, 0, 10, 100, -100] [-100, 0, 1, 3, 6, 9, 10, 100] 

Application scenarios

It is used to sort the items in the list. The typical code 1 is a sort method of the List itself, which is automatically sorted in ascending order and in the same place. The sorted list itself is modified; typical Code 2 is the built-in function sort called, which generates a new sorted list object. The original list is not affected. The parameters accepted by the two methods are almost the same. They all accept a key parameter, which is used to specify which part of the object is used as the basis for sorting:

Data_list = [(0,100), (77, 34), (55, 97)] data_list.sort (key = lambda x: x [1]) # We want to sort print (data_list)> [(77, 34), (55, 97), (0,100)] based on the second number of list items

Another frequently used parameter is reverse, which is used to specify whether to sort in descending order. The default value is False:

Data_list = [(0,100), (77, 34), (55, 97)] data_list.sort (key = lambda x: x [1], reverse = True) # We want to sort the list items based on the second number and print (data_list) in reverse order >>> [(0,100), (55, 97), (77, 34)]

Benefits

1. built-in sorting method, high execution efficiency, strong table ability, make the code more compact, read

2. Flexible parameters are used to specify the benchmark for sorting, which is much easier than writing a comparator in a language similar to Java.

Other Instructions

1. sorted built-in functions have a wider application scope than the sort method of the list. They can sort non-list iteratable data structures;

2. the built-in sort method of list, which belongs to In-situ sorting, can theoretically save memory consumption;

Summary

Well, the above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, you can leave a message, thank you for your support.

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.