如何用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。

示例

以下程式使用集合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]

結論

在本文中,我們學習了四種不同的方法來從第一個列表和第二個列表中移除相同的元素。我們還學習瞭如何將列表轉換為集合,以及如何將集合轉換為列表。我們學習瞭如何建立淺複製以及如何迭代可迭代物件的淺複製(列表)。

更新於:2022年10月27日

2K+ 閱讀量

啟動你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.