如何在 Python 中連線列表的列表?


在 Python 中,列表是有序序列,可以包含多種物件型別,例如整數、字元或浮點數。

在本文中,我們將向您展示如何使用 Python 連線列表的列表(巢狀列表)。現在我們看到完成此任務的 4 種方法:

  • 使用巢狀 for 迴圈

  • 使用列表推導式

  • 使用 sum() 函式

  • 使用 NumPy 模組

假設我們已經獲取了一個包含一些元素的列表的列表。我們將連線這些列表的列表並使用上面指定的不同方法返回結果。

方法 1:使用巢狀 for 迴圈

演算法(步驟)

  • 建立一個變數來儲存輸入的列表的列表(巢狀列表)。

  • 建立一個新的空列表來儲存結果列表

  • 使用 for 迴圈,使用 len() 函式遍歷輸入列表的列表的長度(len() 方法返回物件中的專案數)

  • 再使用一個 for 迴圈遍歷巢狀列表中的每個元素

  • 使用 append() 函式(在末尾將元素新增到列表中)將此元素新增到結果列表中。

  • 連線輸入列表的列表後列印結果列表。

示例

以下程式使用巢狀 for 迴圈連線輸入列表的列表後返回該列表:

# input list of lists (nested list) input_nestedlist = [[1, 3],[2, 6, 7],[9, 5, 12, 7]] print(input_nestedlist) # creating a new empty list for storing result resultList = [] # Traversing in till the length of the input list of lists for m in range(len(input_nestedlist)): # using nested for loop, traversing the inner lists for n in range (len(input_nestedlist[m])): # Add each element to the result list resultList.append(input_nestedlist[m][n]) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists = ", resultList)

輸出

執行上述程式後,將生成以下輸出:

[[1, 3], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining the list of lists = [1, 3, 2, 6, 7, 9, 5, 12, 7]

方法 2:列表推導式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個變數來儲存輸入的列表的列表(巢狀列表)。

  • 使用 列表推導式透過連線巢狀列表的所有元素來建立一個新列表

When you want to create a new list based on the values of an existing list, list comprehension provides a concise syntax.
  • 連線輸入列表的列表後列印結果列表。

示例

以下程式使用列表推導式連線輸入列表的列表後返回該列表:

# input list of lists (nested list) input_list = [["tutorialspoint", "python"], [2, 6, 7], [9, 5, 12, 7]] print(input_list) # Getting each element from nested Lists and storing them in a new list using list comprehension resultList = [element for nestedlist in input_list for element in nestedlist] # printing the resultant list after joining the list of lists print("Resultant list after joining list of lists = ", resultList)

輸出

執行上述程式後,將生成以下輸出:

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5, 12, 7]]
Resultant list after joining list of lists = ['tutorialspoint', 'python', 2, 6, 7, 9, 5, 12, 7]

方法 3:使用 sum() 函式

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 sum() 函式透過傳遞一個空列表作為第二個引數來將巢狀列表連線到單個列表。

sum() 函式返回一個數字,表示可迭代物件中所有專案的總和。

語法

sum(iterable, start)

引數

iterable(可選)- 任何序列,如列表、元組等

start(可選)- 附加/新增到返回值的值

  • 連線輸入列表的列表後列印結果列表。

示例

以下程式使用 sum() 函式連線輸入列表的列表後返回該列表:

# input nested lists input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # pass second argument as empty list to concatenate nested lists resultList = sum(input_listoflists, []) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

輸出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', 2, 6, 7, 9, 5]

方法 4:使用 NumPy 模組

Numpy 庫包含用於連線子字串並將它們展平為單個一維列表的函式。

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 使用 import 關鍵字匯入 NumPy 模組

  • 使用 concatenate() 函式連線列表的列表,並分別使用 flat 屬性和 list() 函式(轉換為列表)將它們展平為一維列表

  • 連線輸入列表的列表後列印結果列表。

示例

以下程式使用 NumPy 模組連線輸入列表的列表後返回該列表:

# importing numpy module import numpy # input list of lists (nested list) input_listoflists = [["tutorialspoint", "python"], [2, 6, 7],[9, 5]] print(input_listoflists) # joining the list of lists using the concatenate() function and # flattening them into a 1-Dimensional list using the flat attribute # and list() function respectively resultList = list(numpy.concatenate(input_listoflists).flat) # printing the resultant list after joining the list of lists print("Resultant list after joining the list of lists:\n", resultList)

輸出

[['tutorialspoint', 'python'], [2, 6, 7], [9, 5]]
Resultant list after joining the list of lists:
['tutorialspoint', 'python', '2', '6', '7', '9', '5']

結論

從本文中,我們學習瞭如何使用四種不同的方法將列表的列表連線/合併為一維列表,包括 for 迴圈、列表推導式、NumPy 函式和 sum() 函式。我們還了解了當我們將巢狀的列表的列表與空列表一起傳遞給 sum() 時會發生什麼。

更新於: 2022-09-19

27K+ 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

開始
廣告