列表基本使用

列表基礎

  • 列表基本使用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # 聲明列表
    x = [1, 2, 3, 'Hello 1', 'Hello 2']

    # 遍歷列表
    for item in x:
    print(item, end = ' ') # 1 2 3 Hello 1 Hello 2

    # 根據索引獲取列表元素
    print(x[2]) # 3
    print(x[3]) # Hello 1
    print(x[-1]) # Hello 2
    print(x[-4]) # 2

    # 獲取列表長度
    len(x) # 5

列表切片

  • 使用:講列表切割:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    x = [1, 2, 3, 'Hello 1', 'Hello 2', 'Hello 3']

    x[0:3] # [1, 2, 3]
    x[3:6] # ['Hello 1', 'Hello 2', 'Hello 3']
    x[:3] # [1, 2, 3]
    x[3:] # ['Hello 1', 'Hello 2', 'Hello 3']
    x[:] # [1, 2, 3, 'Hello 1', 'Hello 2', 'Hello 3']

    # 設置列表路徑的跨度
    x[2:6:2] # [3, 'Hello 2']
    x[::2] # [1, 3, 'Hello 2']
    x[::-2] # ['Hello 3', 'Hello 1', 2]
    x[::-1] # 列表倒序輸出

列表的方法

添加 Add

  1. append()方法:在列表末尾添加元素

    1
    2
    x = [1, 2, 3, 4, 5]
    x.append('Hello 1') # [1, 2, 3, 4, 5, 'Hello 1']
  2. extend()方法:添加一個可迭代對象

    1
    2
    x = [1, 2, 3, 4, 5]
    x.extend(['Hello 1', 'Hello 2', 'Hello 3']) # [1, 2, 3, 4, 5, 'Hello 1', 'Hello 2', 'Hello 3']
  3. 使用列表切片的方式在末尾添加可迭代對象

    1
    2
    x = [1, 2, 3, 4, 5]
    x[len(x):] = ['Hello 1', 'Hello 2'] # [1, 2, 3, 4, 5, 'Hello 1', 'Hello 2']
  4. insert()方法:在列表指定位置插入元素

    1
    2
    x = [1, 3, 4]
    x.insert(1, 'Hello 2') # [1, 'Hello 2', 3, 4]

刪除 Remove

  1. remove()方法:刪除列表中的指定元素

    1
    2
    x = [1, 2, 3, 4]
    x.remove(2) # [1, 3, 4]
    • 如果列表中存在多個匹配的元素,則只會刪除第一個
    • 如果指定的元素不存在則報錯
  2. pop()方法:刪除列表中指定位置的元素,並返回刪除的元素

    1
    2
    x = [1, 2, 3 ,4 ,5]
    x.pop(3) # [1, 2, 3, 5]
  3. clear()方法:清空列表

    1
    2
    x = [1, 2, 3, 4]
    x.clear() # []

更改 Update

  1. 直接通過索引值更改元素

    1
    2
    x = [1, 2, 3, 4]
    x[2] = 'Hello 1' # [1, 2, 'Hello 1', 4]
  2. 通過列表切片一次性更改多個元素

    1
    2
    x = [1, 2, 3, 4, 5]
    x[3:] = ['Hello 1', 'Hello 2'] # [1, 2, 3, 'Hello 1', 'Hello 2']
  3. 在不知道某個元素的索引值時替換為新元素

    1
    2
    x = [1, 2, 3, 4, 5]
    x[x.index(4)] = 'Hello 1' # [1, 2, 3, 'Hello 1', 5]
  1. count()方法:查找某個元素出現的次數

    1
    2
    3
    4
    x = [9, 8, 'Hello 1', 3, 5, 3, 3, 1, 'Hello 1']
    x.count(3) # 3
    x.count('Hello 1') # 2
    x.count(4) # 0
  2. index()方法:查找某個元素的索引值

    1
    2
    3
    x = [9, 8, 'Hello 1', 3, 5, 3, 3, 1, 'Hello 1']
    x.index(5) # 4
    x.index('Hello 1') # 2
    • 如果列表中存在多個匹配的元素,則只會刪除第一個
    • 如果指定的元素不存在則報錯
  3. index(x, start, end):可選參數,指定查找的開始與結束位置

    1
    2
    3
    x = [9, 8, 'Hello 1', 3, 5, 3, 3, 1, 'Hello 1']
    x.index(3, 4, 8) # 5
    x.index('Hello 1', 3) # 8

其他方法

  1. sort()方法:對列表進行排序(倒序)

    1
    2
    3
    x = [3, 1, 4, 6, 2, 9, 3, 5]
    x.sort() # [1, 2, 3, 3, 4, 5, 6, 9]
    x.sort(reverse = True) # [9, 6, 5, 4, 3, 3, 2, 1]
  2. reverse()方法:反轉列表

    1
    2
    x = [3, 1, 4, 6, 2, 9, 3, 5]
    x.reverse() # [5, 3, 9, 2, 6, 4, 1, 3]
  3. copy()方法:複製列表

    1
    2
    x = [3, 1, 4, 6, 2, 9, 3, 5]
    y = x.copy() # y = [3, 1, 4, 6, 2, 9, 3, 5]

列表的加與乘

  1. 加法:
    • 對兩個列表進行拼接
      1
      2
      3
      x = [1, 2, 3]
      y = [4, 5, 6]
      z = x + y # z = [1, 2, 3, 4, 5, 6]
  1. 乘法:
    • 重複列表裡面所有元素若干次
      1
      2
      x = [1, 2, 3]
      z = x * 2 # z = [1, 2, 3, 1, 2, 3]

嵌套列表

  1. 創建嵌套列表:
    • 創建二維列表
      1
      matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  1. 訪問嵌套列表:

    • 遍歷二維列表:

      1
      2
      3
      4
      5
      6
      7
      for i in matrix:
      for item in i:
      print(item, end = ' ')
      print()
      # 1 2 3
      # 4 5 6
      # 7 8 9
    • 使用下標訪問:

      1
      2
      matrix[1]       # [4, 5, 6]
      matrix[1][2] # 6

列表拷貝

列表淺拷貝方法

  1. =方式:

    1
    2
    3
    x = [1, 2, 3, 4]
    y = x
    x[2] = 0 # y = [1, 2, 0, 4]
  2. copy()方法:

    1
    2
    3
    4
    5
    6
    7
    x = [1, 2, 3, 4]
    y = x.copy()
    x [2] = 0 # y = [1, 2, 3, 4]

    x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    y = x.copy()
    x [1][1] = 2 # y = [[1, 2, 3], [4, 2, 6], [7, 8, 9]]
    • copy()方法在嵌套列表中也只是淺拷貝

嵌套列表問題

  • 快速創建二維列表
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    A = [0] * 3             # 0 0 0
    for i in range(3): # 0 0 0
    A[i] = [0] * 3 # 0 0 0

    A[0] is A[1] # False
    A[1] is A[2] # False

    A[1][1] = 1 # 0 0 0
    # 0 1 0
    # 0 0 0

    # 嵌套列表拷貝問題 #
    B = [[0] * 3] * 3 # 0 0 0
    # 0 1 0
    # 0 0 0

    B[0] is B[1] # True
    B[1] is B[2] # True

    B[1][1] = 1 # 0 1 0
    # 0 1 0
    # 0 1 0
    • 由於[[...]] * 3的複製只是複製嵌套在內列表的地址值

copy模塊

  • copy.copy()方法:實現淺拷貝

    1
    2
    3
    4
    import copy
    x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    y = copy.copy(x)
    x[1][1] = 0 # y = [[1, 2, 3], [4, 0, 6], [7, 8, 9]]
  • copy.deepcopy()方法:實現深拷貝

    1
    2
    3
    4
    import copy
    x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    y = copy.deepcopy(x)
    x[1][1] = 0 # y = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

列表推導式

基本語法

  • [expression for target in iterable]
  • for target in iterable - 獲得列表的每一個元素
  • expression - 表達式,對每一個元素統一進行修改
  • 具體編碼:
    1
    2
    3
    4
    5
    6
    x = [1, 2, 3, 4, 5]

    x = [i * 2 for i in x] # [2, 4, 6, 8, 10]

    for i in range(len(x)):
    x[i] *= 2 # [2, 4, 6, 8, 10]

其他範例

  1. 將字符串的字符轉為Unicode編碼並存儲到列表

    1
    x = [ord(c) for c in 'Hello']     # [72, 101, 108, 108, 111]
  2. 提取二維列表中第二列(second column)的元素

    1
    2
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    sec_col = [row[1] for row in matrix] # [2, 5, 8]
  3. 提取二維列表中對角線上的元素

    1
    2
    3
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    diag1 = [matrix[i][i] for i in range(len(matrix))] # [1, 5, 9]
    diag2 = [matrix[i][(len(matrix) - 1) - i] for i in range(len(matrix))] # [3, 5, 7]
  4. 列表推導式創建列表

    1
    2
    3
    4
    S = [[0] * 3 for i in range(3)]
    S[1][1] = 1 # 0 0 0
    # 0 1 0
    # 0 0 0

列表推導式條件語句

  • [expression for target in iterable if condition]
  • if condition - 用於篩選的條件語句
  • 具體編碼:
    1
    2
    3
    4
    5
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    even = [i for i in x if i % 2 == 0] # [2, 4, 6, 8, 10]

    str = ['Donald', 'Tina', 'John', 'Tony', 'May', 'Tom']
    t = [name for name in str if name[0] == 'T'] # ['Tina', 'Tony', 'Tom']

嵌套的列表推導式

  • 語法:

    1
    2
    3
    4
    [expression for target1 in iterable1
    for target2 in iterable2
    ...
    for targetN in iterableN]
  • 將二維列表降級為一維列表:

    1
    2
    3
    4
    5
    6
    7
    8
    # 列表推導式實現
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    flatten = [col for row in matrix for col in row]

    # 循環實現
    for row in matrix:
    for col in row:
    flatten.append(col)