Python 3.1 快速導覽 - 串列型態的 sort()

串列 (list) 型態 (type) 的 sort() 方法 (method) ,排序 list 中的元素

方法描述
list.sort([key[, reverse]])排序 list 中的元素


舉例示範如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
a = [3, 6, 1, 4, 9, 7, 2, 5, 8]
print(a)
a.sort()
print(a)
print()
 
b = [3, 6, 1, 4, 9, 7, 2, 5, 8]
print(b)
b.sort(reverse=True)
print(b)
print()
 
c = ["b", "a", "g", "d", "c", "e", "f", "h"]
print(c)
c.sort()
print(c)
print()
 
c = ["b", "a", "g", "d", "c", "e", "f", "h"]
print(c)
c.sort(reverse=True)
print(c)
print()
 
# 《程式語言教學誌》的範例程式
# 檔名:listsort.py
# 功能:示範 Python 程式
# 作者:張凱慶
# 時間:西元 2010 年 12 月


執行結果如下



中英文術語對照
串列list
型態type
方法method


內建型態




沒有留言: