Python 基礎語法四-list 與 tuple
list
list(列表)是有序、可變的資料集合,可以隨時新增、刪除和修改元素,同時也是 python 內建的一種資料型別。
在 python 中使用一對方括號[]
來定義一個 list。list 中的元素可以是任意的資料型別,甚至元素可以是一個 list。
list 可以看做是 C 或 java 中的陣列,list 和陣列最大的不同是 list 中的元素可以是不同的資料型別,而陣列在定義後要求內部元素的資料型別必須與定義時的資料型別相同。
下面是一個 list 的定義
>>> a = [] >>> type(a) <class 'list'> >>> print(a) [] >>> b = [1, 'str', 3.14, a] >>> print(b) [1, 'str', 3.14, []] >>> bool(a) False >>> bool(b) True
list 基本操作
-
len()
len() 用來計算 list 的長度,在互動模式下使用示例
>>> a = [] >>> list(a) 0 >>> b = [1, 'str', 3.14, a] >>> list(b) 4
-
+
+ 用來連線兩個 list,在互動模式下使用示例
>>> a = ['python', 'keinYe'] >>> b = [1, 2, 3, 4] >>> a + b ['python', 'keinYe', 1, 2, 3, 4]
-
*
* 用來對一個 list 進行多次重複,在互動模式下使用示例
>>> a = ['python', 'keinYe'] >>> a * 2 ['python', 'keinYe', 'python', 'keinYe']
-
in
in 用來判斷元素是否包含在 list 中,在互動模式下使用示例
>>> a = ['python', 'keinYe'] >>> 'python' in a True >>> 'mac' in a False
-
max 和 min
max 和 min 用來獲取 list 中的最大值和最小值,在互動模式下使用示例
>>> a = [1, 3, 2, 6, 4, 9] >>> max(a) 9 >>> min(a) 1
-
增加和刪除元素
append 和 insert向 list 中增加元素.
append 用來向 list 末尾增加新的元素,insert 可以在 list 的指定位置增加新的元素。
>>> a = [1, 2, 3] >>> a.append(4) >>> a [1, 2, 3, 4] >>> a.insert(1, 5) >>> a [1, 5, 2, 3, 4]
pop 和 remove從 list 中刪除元素
remove 用來刪除指定的元素,pop 用來刪除末尾或指定位置的元素,同時返回被刪除的元素。
>>> a = [1, 2, 3, 4, 5, 6, 7] >>> a.remove(1) >>> a [2, 3, 4, 5, 6, 7] >>> a.remove(8) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: list.remove(x): x not in list >>> a.pop() 7 >>> a [2, 3, 4, 5, 6] >>> a.pop(1) 3 >>> a [2, 4, 5, 6]
使用 remove 來刪除 list 中的元素是,若 list 中不存在該元素,則會報錯,故在使用 remove 前要先判斷元素存在於 list 中。
list 索引
下標索引(類似C中陣列索引),反向索引下標為負值。在 list 中索引是以元素為單位進行的
>>> a = [1, 'python', 3.14] >>> a[0] 1 >>> a[3] raceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range >>> a[len(a)-1] 3.14 >>> a[-1] 3.14 >>> a[-2] 'python' >>> a[-3] 1
list 的索引範圍為 0 到 len(list) - 1 或者 -1 至 -len(list),當索引超出範圍時,python 會報 IndexError 錯誤。
使用下標索引也可以替換 list 中的元素
>>> a = [1, 'python', 3.14] >>> a[0] = 5 >>> a [5, 'python', 3.14] >>> a[1] = 10 >>> a [5, 10, 3.14]
從以上示例可以看出使用 list 下標索引不僅可以更改 list 的元素值,還可以使用不同的資料型別來替換 list 中的元素。
物件有型別,變數無型別。list 中元素的資料型別是動態可變的。
list 排序
可以使用 sort 函式來對 list 進行排序。
sort(*, key=None, reverse=False) This method sorts the list in place, using only < comparisons between items. Exceptions are not suppressed - if any comparison operations fail, the entire sort operation will fail (and the list will likely be left in a partially modified state).
>>> a = [2, 5, 9, 4, 1, 3, 8, 6] >>> a.sort() >>> a [1, 2, 3, 4, 5, 6, 8, 9]
sort 函式預設對 list 進行升序排列, list.sort() 是對列表進行原地修改,沒有返回值。
由於 sort 函式預設是對 list 機型升序排列,有時我們需要對 list 進行降序排列,這時就用到了 sort 函式的 reverse 引數
>>> a = [2, 5, 9, 4, 1, 3, 8, 6] >>> a.sort(reverse=True) >>> a [9, 8, 6, 5, 4, 3, 2, 1]
tuple
tuple (元組)也是一種有序列表,和 list 不同的是 tuple 一旦初始化就不能修改
元組是用圓括號括起來的,其中的元素之間用逗號隔開。
>>> tuple = (1, 'python', [1, 2, 3]) >>> type(tuple) <class 'tuple'> >>> tuple (1, 'python', [1, 2, 3])
元組初始化後不能修改,誤修改時 python 會報TypeError
錯誤。
>>> tuple = (1, 'python', [1, 2, 3]) >>> tuple[0] = 2 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
建立空的元組
>>> tuple = () >>> tuple ()
建立僅有一個元素的元組
>>> tuple = (1,) >>> tuple (1,)