Python Learning Tips list item Sort sample code share

Source: Internet
Author: User
Tags python list
This article is mainly to introduce the Python learning tips of the list item sorting information, the article introduced in very detailed, the need for friends can learn from, follow the small series together to study it.

This article is about the Python list item sorting related content, share out for everyone to refer to the study, below to see the detailed introduction:

Typical code 1:

Data_list = [6, 9, 1, 3, 0, ten, -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, ten, -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

Used when you want to sort items in a list. Where the typical code 1 is the list itself using a sort method, the method is automatically sorted in ascending order, and is sorted in place, the sorted list itself will be modified; A typical code of 2 is a call to the built-in function sort, which produces a new sorted list object, and the original list is unaffected. These two methods accept parameters that are almost identical, and they all accept a key parameter, which is used to specify which part of the object to sort by:

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

Another frequently used parameter is reverse, which is used to specify whether to sort in reverse order and false by default:

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

Benefits to be brought

1. Built-in sequencing method, high execution efficiency, strong expression ability, make the code more compact, read

2. Flexible parameters, used to specify the benchmark for sorting, much more convenient than writing a comparator in a Java-like language

Other instructions

1. The sorted built-in function is more broadly applicable than the list's sort method, and it can sort the structure of an iterative data except for the list;

2. The list's built-in sort method, which belongs to in-situ sequencing, can theoretically save memory consumption;

Summarize

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.