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


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

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

  • 使用巢狀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年9月19日

27K+ 次瀏覽

啟動你的職業生涯

完成課程獲得認證

開始
廣告