Python – 在列表中替換子列表


在本文中,我們將瞭解如何用新列表替換子列表的值。在處理列表操作時,您可能遇到過這個問題。在這裡,我們將看到可以使用各種方法將子列表替換為其他列表。

讓我們透過以下示例來了解這一點:

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]

這裡我們有一個名為 listItem 的列表,其中包含一些元素,還有一個名為 new_list 的列表。所以,我們想用 new_list 替換 listItem 的子列表。取索引範圍為 2 到 5,如果我們用 new_list 的值替換這些索引值,那麼我們的 listItem 將變為。

listItem : [1, 2, 7, 8, 9, 6]

讓我們看看執行此操作以用 new_list 替換子列表的方法。

方法 1. 使用 split() 和 join() 函式。

在這種方法中,我們使用列表的切片和連線來用另一個新列表替換子列表。

示例

def replace_sublist(listItem, start, end, new_list):
   return listItem[:start] + new_list + listItem[end:]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用切片來獲取子列表之前和之後的部分。然後,我們將這些部分連線起來,這些部分是子列表之前的部分、新列表和子列表之後的部分。從示例中您可以看到,我們將索引範圍指定為 2 到 5,並將新列表指定為 [7,8,9],因此我們的列表將在索引 [2..4] 處更新,我們的新列表將為 [1, 2, 7, 8, 9, 6]。

方法 2. 使用列表推導式。

此方法提供了一種使用現有列表遍歷和建立列表的簡便方法。

示例

def replace_sublist(listItem, start, end, new_list):
   return [new_list[i - start] if start <= i < end else element for i, element in enumerate(listItem)]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用列表推導式技術使用 enumerate() 方法遍歷原始列表。然後,我們用新列表替換 listItem 子列表元素。

方法 3. 使用列表賦值。

示例

def replace_sublist(listItem, start, end, new_list):
   listItem[start:end] = new_list
   return listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用直接列表賦值技術直接將新列表賦值給子列表位置。我們使用 listItem[start : end] 將新列表賦值給子列表位置,Python 語言將自動用新列表替換子列表。與其他方法相比,這是一種非常簡單的方法。

方法 4. 使用列表切片和 sum 運算子。

示例

def replace_sublist(listItem, start, end, new_list):
   return listItem[:start] + new_list + listItem[end:]

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用列表切片方法和 sum (+) 運算子來替換子列表。我們提取子列表之前和之後的部分,然後使用 sum (+) 運算子連線所有這些部分。

方法 5. 使用列表複製、清除和擴充套件。

示例

def replace_sublist(listItem, start, end, new_list):
   copied_listItem = listItem.copy()
   del copied_listItem[start:end]
   copied_listItem.extend(new_list)
   return copied_listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用 list.copy() 方法建立主列表的副本,然後使用 del copied_listItem[start:end] 從複製的列表中刪除給定範圍的子列表。然後,我們使用 copied_listItem.extend(new_list) 將複製列表擴充套件為新列表元素。透過執行這些步驟,子列表將被 new_list 專案替換。

方法 6. 使用列表索引。

示例

def replace_sublist(listItem, start, end, new_list):
   for i in range(start, end):
      listItem[i] = new_list[i - start]
   return listItem

listItem = [1, 2, 3, 4, 5, 6]
new_list = [7, 8, 9]
updated_list = replace_sublist(listItem, 2, 5, new_list)
print("Original list: ", listItem)
print("new list to update is: ", new_list)
print("Updated list after replacing sublist: ",updated_list)

輸出

Original list: [1, 2, 3, 4, 5, 6] 
new list to update is: [7, 8, 9] 
Updated list after replacing sublist: [1, 2, 7, 8, 9, 6]

解釋

在上面的示例中,我們使用列表索引來替換子列表項。我們遍歷子列表元素範圍 range(start, end) 的索引範圍,然後使用 listItem[i] = new_list[i - start] 將子列表元素替換為 new_list 的元素。從示例中您可以看到,從索引 2 到 5 的子列表將被新列表項替換,因此我們的列表將在索引 [2..4] 處更新,我們的新列表將為 [1, 2, 7, 8, 9, 6]。

因此,我們瞭解了使用其他 new_list 替換子列表的各種方法。我們看到了解決問題的各種方法,包括列表切片、列表推導式、列表索引、split() 方法,以及許多其他可以替換這些子列表的方法。每種方法都有其獨特的解決問題的方法,您可以根據自己的需要選擇任何合適且簡單的方法。

更新於:2023年10月3日

瀏覽量 198

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告