Latest web development tutorials

Python3 List sort()方法

Python3 列表 Python3列表


描述

sort() 函數用於對原列表進行排序,如果指定參數,則使用比較函數指定的比較函數。

語法

sort()方法語法:

list.sort([func])

參數

  • func -- 可選參數, 如果指定了該參數會使用該參數的方法進行排序。

返回值

該方法沒有返回值,但是會對列表的對象進行排序。

實例

以下實例展示了sort()函數的使用方法:

#!/usr/bin/python3

list1 = ['Google', 'w3big', 'Taobao', 'Baidu']
list1.sort()
print ("列表排序后 : ", list1)

以上實例輸出結果如下:

列表排序后 :  ['Baidu', 'Google', 'w3big', 'Taobao']

Python3 列表 Python3列表