定義:一種數據結構,一個序列,用於順序的存儲數據python
序列在Python中是最基本的數據結構。序列中的每一個元素都分配一個數字,就是它的位置,也叫索引,第一個索引時0,第二個索引是1,以此類推。算法
列表中的元素是object對象。數據結構
例子:app
In [1]: lst = list(range(1, 10)) In [2]: lst Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In [3]: lst[0] Out[3]: 1 In [4]: len(lst) Out[4]: 9 In [5]: lst[8] Out[5]: 9 In [6]: lst[-1] Out[6]: 9 In [7]: lst[-2] Out[7]: 8
index() 函數用於從列表中找出某個值第一個匹配項的索引位置函數
In [8]: help(list.index) Help on method_descriptor: index(...) L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. ~ ~ (END)
In [9]: lst Out[9]: [1, 2, 3, 4, 5, 6, 7, 8, 9] In [10]: lst = [1, 2, 3, 2, 3, 2, 1] In [11]: lst.index(2) Out[11]: 1 In [12]: lst.index(2, 2) Out[12]: 3 In [13]: lst.index(2, 2, 4) Out[13]: 3
list.index 的start 和 end 參數是能夠爲負數的,可是 在查找的過程當中仍是從左往右的順序ui
count() 方法用於統計某個元素在列表中出現的次數spa
In [20]: help(list.count) Help on method_descriptor: count(...) L.count(value) -> integer -- return number of occurrences of value ~ (END)
In [25]: lst Out[25]: [1, 2, 3, 2, 3, 2, 1] In [26]: lst.count(3) Out[26]: 2 In [27]: lst.count(2) Out[27]: 3
對列表作查詢code
index 和 count 方法的時間複雜度都是O(n)對象
線性複雜度:效率與數據規模 呈線性相關排序
In [28]: lst Out[28]: [1, 2, 3, 2, 3, 2, 1] In [29]: lst[2] = 5 In [30]: lst Out[30]: [1, 2, 5, 2, 3, 2, 1] In [31]: lst[10] = 10 --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-31-6a600afa3b95> in <module>() ----> 1 lst[10] = 10 IndexError: list assignment index out of range
In [32]: help(list.append) # 追加 Help on method_descriptor: append(...) L.append(object) -> None -- append object to end ~ (END)
In [33]: lst = list(range(12)) In [34]: lst Out[34]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [35]: lst.append(12) In [36]: lst Out[36]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] In [37]:
In [37]: help(list.insert) Help on method_descriptor: insert(...) L.insert(index, object) -- insert object before index ~ (END)
In [45]: lst = list(range(12)) In [46]: lst Out[46]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [47]: lst.insert(1, 'papapa') In [48]: lst Out[48]: [0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [49]: lst.insert(0, 'micracle') In [50]: lst Out[50]: ['micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [51]: lst.insert(-1, '-1') In [52]: lst Out[52]: ['micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '-1', 11] In [53]: lst.insert(-100, '-100') In [54]: lst Out[54]: ['-100', 'micracle', 0, 'papapa', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, '-1', 11] In [55]:
index 與 insert 方法,往左邊的位置添加元素
append 和 insert 的效率
儘可能使用append
extend使用一個序列擴展另外一個list,參數是序列。序列中的元素將逐項添加到列表的尾部。
In [55]: help(list.extend) Help on method_descriptor: extend(...) L.extend(iterable) -> None -- extend list by appending elements from the iterable ~ (END)
In [56]: lst = list(range(4)) In [57]: lst Out[57]: [0, 1, 2, 3] In [58]: lst.extend('a') In [59]: lst Out[59]: [0, 1, 2, 3, 'a'] In [60]: lst.extend([1, 2, 4]) In [61]: lst Out[61]: [0, 1, 2, 3, 'a', 1, 2, 4] In [62]: lst.extend([1], [2]) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-62-fe0391927e26> in <module>() ----> 1 lst.extend([1], [2]) TypeError: extend() takes exactly one argument (2 given) In [63]:
extend 將任意可迭代對象追加到數據的末尾,原地修改,返回None
In [64]: lst.append([1]) In [65]: lst Out[65]: [0, 1, 2, 3, 'a', 1, 2, 4, [1]] In [66]: lst.extend([1]) In [67]: lst Out[67]: [0, 1, 2, 3, 'a', 1, 2, 4, [1], 1] In [68]:
時間複雜度:定性描述一個算法的效率,是用來分析的,不是來計算
In [72]: help(list.remove) Help on built-in function remove: remove(...) method of builtins.list instance L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. ~ (END)
In [73]: lst = [1, 2, 3, 2, 1] In [74]: lst .remove(2) In [75]: lst Out[75]: [1, 3, 2, 1] In [76]: lst.remove('a') --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-76-054406dca733> in <module>() ----> 1 lst.remove('a') ValueError: list.remove(x): x not in list In [77]:
In [77]: help(list.pop) Help on method_descriptor: pop(...) L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. ~ (END)
In [81]: lst = ['a', 'b', 'c'] In [82]: lst.pop() Out[82]: 'c' In [83]: lst Out[83]: ['a', 'b'] In [84]: lst.pop() Out[84]: 'b' In [85]: lst.pop() Out[85]: 'a' In [86]: lst.pop() --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-86-a1e347685b13> in <module>() ----> 1 lst.pop() IndexError: pop from empty list In [87]: lst = ['a', 'b', 'c'] In [88]: lst.pop(1) Out[88]: 'b' In [89]: lst Out[89]: ['a', 'c']
In [90]: help(list.clear) # 刪除全部元素 Help on method_descriptor: clear(...) L.clear() -> None -- remove all items from L ~ (END)
In [92]: lst = list(range(10)) In [93]: lst Out[93]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [94]: lst.clear() In [95]: lst Out[95]: [] In [96]:
In [96]: lst = list(range(10)) In [97]: lst Out[97]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [98]: len(lst) # 統計容器內的項目數量並返回 Out[98]: 10 In [99]:
reverse() 函數用於反向列表中元素。
In [103]: lst = list(range(10)) In [104]: lst Out[104]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [105]: lst.reverse() # 原地修改,返回None In [106]: lst Out[106]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] In [107]:
In [109]: lst = [1, 2, 4, 6, 8, 9, 3, 5] In [110]: lst.sort() # 排序, 原地修改,返回None In [111]: lst Out[111]: [1, 2, 3, 4, 5, 6, 8, 9] In [112]:
賦值操做是引用傳遞,也叫淺複製,淺拷貝
In [114]: help(list.copy) Help on method_descriptor: copy(...) L.copy() -> list -- a shallow copy of L (END)
In [115]: l = list(range(12)) In [116]: l Out[116]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [117]: id(l) Out[117]: 140547292818120 In [118]: m = l.copy() # 淺拷貝 影子拷貝 In [119]: m Out[119]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] In [120]: id(m) Out[120]: 140547292186248 In [121]:
In [126]: from copy import deepcopy In [127]: help(deepcopy) Help on function deepcopy in module copy: deepcopy(x, memo=None, _nil=[]) Deep copy operation on arbitrary Python objects. See the module's __doc__ string for more info. ~ END
In [141]: l = [1, [2, 3], 4] In [142]: m = l.copy() In [143]: m[1][1] = 100 In [144]: m Out[144]: [1, [2, 100], 4] In [145]: l Out[145]: [1, [2, 100], 4] In [146]: n = deepcopy(l) In [147]: n Out[147]: [1, [2, 100], 4] In [148]: n[1][1] = 1000 In [149]: n Out[149]: [1, [2, 1000], 4] In [150]: l Out[150]: [1, [2, 100], 4] In [151]:
元組也是一種有序列表:tuple
list 和 tuple 的區別:
In [151]: classmates = ('miracle', 'tt', 'kk') In [152]: classmates Out[152]: ('miracle', 'tt', 'kk') In [153]: classmates[0] Out[153]: 'miracle' In [154]: classmates[3] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-154-81a27e3ce05f> in <module>() ----> 1 classmates[3] IndexError: tuple index out of range
append,insert,extend都沒有
()既是括號,又是元組
In [155]: (1) Out[155]: 1 In [156]: t = ('a', 'b', ['A', 'B']) In [157]: t[2][0] = 'X' In [158]: t Out[158]: ('a', 'b', ['X', 'B']) In [159]: t[2] = 1 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-159-45549d36daea> in <module>() ----> 1 t[2] = 1 TypeError: 'tuple' object does not support item assignment In [160]: