Python 列表排序詳解
發(fā)布日期:2021-12-27 01:27 | 文章來源:CSDN
在Python中,對(duì)列表進(jìn)行排序有兩種方法。
一種是調(diào)用sort()方法,該方法沒有返回值,對(duì)列表本身進(jìn)行升序排序。
cars = ['bmw', 'audi', 'toyota', 'subaru'] cars.sort() print(cars)
輸出:
['audi', 'bmw', 'subaru', 'toyota']
另一種方法是使用sorted()函數(shù),該函數(shù)會(huì)返回升序排序的列表,同時(shí)不影響原本的列表。
cars = ['bmw', 'audi', 'toyota', 'subaru'] print("Here is the original list:") print(cars) print("\nHere is the sorted list:") print(sorted(cars)) print("\nHere is the original list again:") print(cars)
輸出:
Here is the original list: ['bmw', 'audi', 'toyota', 'subaru'] Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota'] Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru']
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
相關(guān)文章