如何在Python中將列表新增到另一個列表(連線列表)?


在Python中,列表是一個有序的序列,可以包含多種物件型別,例如整數、字元或浮點數。在其他程式語言中,列表相當於陣列。

在本文中,我們將學習如何在Python中將一個列表新增到另一個列表(連線列表)。以下是完成此任務的不同方法:

  • 使用連線(+)運算子

  • 使用列表物件的append方法

  • 使用extend()方法

  • 使用itertools.chain()方法

  • 獲取不包含重複項的列表

假設我們已經獲取了一個包含一些元素的列表。我們將使用上述方法返回給定兩個輸入列表的連線列表。

方法1:使用連線(+)運算子

連線運算子(+)是連線Python列表最常用的方法。如下面的示例所示,"+"運算子可以輕鬆地將整個列表連線到另一個列表後面,並將新列表作為結果輸出。

使用連線運算子(+)將第二個列表新增到第一個列表中,並將其儲存在第一個列表中。

示例

以下程式使用(+)運算子返回給定兩個輸入列表的連線列表:

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

輸出

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

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

方法2:使用列表物件的append()方法

演算法(步驟)

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

  • 建立一個變數來儲存第一個輸入列表,並用一些隨機值初始化它。同樣,建立一個包含另一組隨機值的另一個列表(第二個輸入列表)。

  • 透過將第二個輸入列表作為引數傳遞給append()函式(在列表末尾新增元素)將第二個輸入列表新增到第一個輸入列表。

  • 連線第二個輸入列表後,列印第一個輸入列表。

示例

以下程式使用append()方法返回給定兩個輸入列表的連線列表:

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the append() function givenList1.append(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

輸出

執行上述程式將生成以下輸出。

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, ['python', 'code']]

方法3:使用extend()方法

要連線Python中的兩個列表,可以使用extend()函式。extend()函式迭代給定的引數並將專案新增到列表中,從而線性擴充套件列表。

語法

list.extend(iterable)

示例

以下程式使用extend()方法返回給定兩個輸入列表的連線列表:

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using the extend() function givenList1.extend(givenList2) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

輸出

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

方法4:使用itertools.chain()方法

itertools.chain()函式返回在其引數之後連結的迭代器,因此如果只需要初始迭代,則不需要儲存連線列表。當只需要連線列表一次時,這非常方便。

示例

以下程式使用itertools.chain()函式返回給定兩個輸入列表的連線列表:

import itertools # first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code"] # Adding the second list(Concatenating) to the first list using itertools.chain() function givenList1 = list(itertools.chain(givenList1, givenList2)) # Printing the first List after concatenating it with the second list print ("First list after concatenating with the second list: ", givenList1)

輸出

First list after concatenating with second list: ['Hello', 10, 'TutorialsPoint', 20, 'python', 'code']

方法5:獲取不包含重複項的列表

演算法(步驟)

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

  • 建立一個變數來儲存第一個輸入列表,並用一些隨機值初始化它。同樣,建立一個包含另一組隨機值的另一個列表(第二個輸入列表)。

  • 使用連線運算子(+)將第二個列表新增到第一個列表中,並將其儲存在第一個列表中。

  • 使用set()函式將第一個列表轉換為集合(它會刪除重複項),然後使用list()函式將此集合轉換回列表。

  • 列印結果列表,即不包含重複項的列表。

示例

以下程式返回給定兩個輸入列表的連線列表,其中不包含重複項:

# first input list givenList1 = ["Hello", 10, "TutorialsPoint", 20] # second input list givenList2 = ["python", "code", 20, "TutorialsPoint"] # Adding the second list(Concatenating) to the first list givenList1 = givenList1 + givenList2 # Removing duplicates from the concatenated list uniqueList=list(set(givenList1)) # Printing the concatenated list after removing duplicates print ("Concatenated list after removing duplicates: ", uniqueList)

輸出

Concatenated list after removing duplicates: ['TutorialsPoint', 10, 'Hello', 'python', 20, 'code']

結論

我們學習瞭如何使用四種不同的方法將一個列表新增到另一個列表(列表連線),包括連線運算子(+)、append()、extend()和chain()函式。我們還學習瞭如何從連線列表中刪除重複項。

更新於:2022年9月19日

2K+ 次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告