Python3 list.sort()方法

2019-10-16 23:10:42
sort()方法排序列表中的物件,使用 func 比較(如果給定)。

語法

下面是 sort()方法的語法 -
list.sort([func])

引數

NA

返回值

此方法不返回任何值,但反轉列表中給定的物件。

範例

下面的範例演示 sort()方法的使用。
#!/usr/bin/python3
list1 = ['physics', 'Biology', 'chemistry', 'maths']
list1.sort()
print ("list now : ", list1)
當我們執行上面的程式,會產生以下結果 -
list now :  ['Biology', 'chemistry', 'maths', 'physics']