Latest web development tutorials

Python List sort()方法

Python 列表 Python列表


描述

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

語法

sort()方法語法:

list.sort([func])

參數

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

返回值

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

實例

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

#!/usr/bin/python

aList = [123, 'xyz', 'zara', 'abc', 'xyz'];

aList.sort();
print "List : ", aList;

以上實例輸出結果如下:

List :  [123, 'abc', 'xyz', 'xyz', 'zara']

Python 列表 Python列表