Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法
來源:懂視網(wǎng)
責(zé)編:小采
時(shí)間:2020-11-27 14:30:24
Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法
Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法:1. 原地排序:采用sort()方法,按照指定的順序排列數(shù)據(jù)后用排序后的數(shù)據(jù)替換原來的數(shù)據(jù)(原來的順序丟失),如: 代碼如下:>>> data1=[4,2,6,432,78,43,22,896,42,677,12]>>> data1.sort()>>> data1 #原來的順序被替換[2, 4, 6, 1
導(dǎo)讀Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法:1. 原地排序:采用sort()方法,按照指定的順序排列數(shù)據(jù)后用排序后的數(shù)據(jù)替換原來的數(shù)據(jù)(原來的順序丟失),如: 代碼如下:>>> data1=[4,2,6,432,78,43,22,896,42,677,12]>>> data1.sort()>>> data1 #原來的順序被替換[2, 4, 6, 1

1. 原地排序:采用sort()方法,按照指定的順序排列數(shù)據(jù)后用排序后的數(shù)據(jù)替換原來的數(shù)據(jù)(原來的順序丟失),如:
代碼如下:
>>> 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. 復(fù)制排序:采用sorted()內(nèi)置函數(shù),按照指定的順序排列數(shù)據(jù)后返回原數(shù)據(jù)的一個(gè)有序副本(原來的順序保留),如:
代碼如下:
>>> 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] #對副本排序
>>>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法
Python學(xué)習(xí)筆記_數(shù)據(jù)排序方法:1. 原地排序:采用sort()方法,按照指定的順序排列數(shù)據(jù)后用排序后的數(shù)據(jù)替換原來的數(shù)據(jù)(原來的順序丟失),如: 代碼如下:>>> data1=[4,2,6,432,78,43,22,896,42,677,12]>>> data1.sort()>>> data1 #原來的順序被替換[2, 4, 6, 1