如何使用 Python 刪除列表中重複的元素
在本文中,我們將向您展示如何在 Python 中刪除列表中重複的元素。簡單來說,就是從兩個列表中刪除共同的元素。
以下是完成此任務的各種方法:
- 使用 remove() 函式
- 使用列表推導式
- 使用集合差運算子
- 使用集合 difference() 函式
假設我們有兩個包含一些元素的列表。我們現在將從這兩個列表中刪除相同或共同的元素。
使用 remove() 函式
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存 inputList_1。
建立另一個變數來儲存 inputList_2。
使用 for 迴圈,透過使用 [:] 符號建立副本,遍歷 inputList_1(這將建立原始列表的淺複製,同時保留副本中所有物件的引用)。
使用 if 條件語句,使用 in 運算子檢查 inputList_1 中的元素是否出現在 inputList_2 中。
使用 remove() 方法(刪除具有給定值的元素的第一個出現)從 inputList_1 和 inputList_2 中刪除相應的元素。
刪除相同或共同元素後,列印 InputList_1。
示例
以下程式使用 remove() 函式從兩個列表中刪除相同或共同的元素,並返回結果列表。
# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # traversing in input list 1( Here [:] is used to create shallow copy) for element in inputList_1[:]: # Checking whether the element in list1 is present in list2 if element in inputList_2: # removing that element from both the inputList_1 & inputList_2 inputList_1.remove(element) inputList_2.remove(element) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)
輸出
執行上述程式後,將生成以下輸出:
InputList_1 after removing same elements: [3, 1]
使用列表推導式
以下程式使用列表推導式從兩個列表中刪除相同或共同的元素,並返回結果列表。
# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # Creating a new list containing elements that are not in the second list using the list comprehension inputList_1 = [p for p in inputList_1 if p not in inputList_2] # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)
輸出
執行上述程式後,將生成以下輸出。
InputList_1 after removing same elements: [3, 1]
使用集合差運算子
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個名為 removeSameElements() 的函式,該函式從作為引數傳遞給它的 inputList_1、inputList_2 中刪除共同的元素。
使用 set() 函式(建立集合物件。集合列表將以隨機順序顯示,因為專案沒有排序。它刪除所有重複項)將兩個給定列表轉換為集合,並計算這兩個集合之間的差值(以從第一個列表中刪除共同的元素),然後使用 list() 函式(將序列/可迭代物件轉換為列表)轉換為列表。
列印上述結果。
透過將 inputList_1、inputList_2 作為引數傳遞來呼叫上面定義的 removeSameElements() 函式。
示例
以下程式使用集合差運算子從兩個列表中刪除相同或共同的元素,並返回結果列表。
# creating a function that removes the common elements from # the both the inputList_1, inputList_2 passed as arguments def removeSameElements(inputList_1, inputList_2): # Converting both lists to set and calculating the set difference between the two # Converting the result back to list inputList_1 = list(set(inputList_1) - set(inputList_2)) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1) # input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # calling the removeSameElements() function by passing both the input lists removeSameElements(inputList_1, inputList_2)
輸出
執行上述程式後,將生成以下輸出:
InputList_1 after removing same elements: [1, 3]
使用集合 difference() 函式
在 Python 中,difference() 方法返回一個包含兩個集合之間差值的集合,即返回的集合包含僅出現在第一個集合中的專案,並排除兩個集合中都存在的專案。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 set() 函式將第一個列表轉換為集合,然後使用 difference() 函式(difference() 方法比較兩個集合並返回第一個集合中唯一的專案)計算此集合與第二個列表之間的差值,最後使用 list() 函式將結果轉換為列表。
刪除相同或共同元素後,列印 InputList_1。
示例
以下程式使用 set difference() 函式從兩個列表中刪除相同或共同的元素,並返回結果列表:
# input list 1 inputList_1 = [3, 6, 7, 1, 2] # input list 2 inputList_2 = [2, 4, 6, 7, 8, 9] # Converting the first list to set and calculate the difference between the second list using the difference() function # Converting the result back to list inputList_1 = list(set(inputList_1).difference(inputList_2)) # printing InputList_1 after removing the same or common elements print("InputList_1 after removing same elements:", inputList_1)
輸出
執行上述程式後,將生成以下輸出:
InputList_1 after removing same elements: [1, 3]
結論
在本文中,我們學習了四種不同的方法來從第一個列表和第二個列表中刪除相同的元素。我們還學習瞭如何將列表轉換為集合,以及如何將集合轉換為列表。我們學習瞭如何建立淺複製以及如何遍歷可迭代物件的淺複製(列表)。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP