Python learning-data sorting method

Source: Internet
Author: User

Python sorts data in two ways:

1. In-situ sorting: the sort () method is used to sort data in the specified order and then replace the original data with the sorted data (the original order is lost), for example:

>>> Data1 = [6,432, 22,896, 42,677, 12] >>> data1.sort () >>> data1 # replace the original sequence with [2, 4, 6, 12, 22, 42, 43, 78,432,677,896]

 

2. Copy sorting: using the built-in sorted () function, Sort data in the specified order and return an ordered copy of the original data (the original order is retained), for example:

>>> Data1 = [6,432, 22,896, 42,677, 6,432, 12] >>> data2 = sorted (data1) >>> data1 [4, 2, 78, 43, 22,896, 42,677, 12] # original order reserved> data2 [2, 4, 6, 12, 22, 42, 43, 78,432,677,896] # Sort copies>

 

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.