Home > Backend Development > Python Tutorial > Python学习笔记_数据排序方法

Python学习笔记_数据排序方法

WBOY
Release: 2016-06-16 08:44:12
Original
1208 people have browsed it

1. 原地排序:采用sort()方法,按照指定的顺序排列数据后用排序后的数据替换原来的数据(原来的顺序丢失),如:

复制代码 代码如下:

>>> data1=[4,2,6,432,78,43,22,896,42,677,12]
>>> data1.sort()
>>> data1       #原来的顺序被替换
[2, 4, 6, 12, 22, 42, 43, 78, 432, 677, 896]

2. 复制排序:采用sorted()内置函数,按照指定的顺序排列数据后返回原数据的一个有序副本(原来的顺序保留),如:

复制代码 代码如下:

>>> data1=[4,2,6,432,78,43,22,896,42,677,12]
>>> data2=sorted(data1)
>>> data1    
[4, 2, 6, 432, 78, 43, 22, 896, 42, 677, 12]    #原顺序保留
>>> data2   
[2, 4, 6, 12, 22, 42, 43, 78, 432, 677, 896]    #对副本排序
>>>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template