如何使用 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]

结论

在本文中,我们学习了四种不同的方法来从第一个列表和第二个列表中删除相同的元素。我们还学习了如何将列表转换为集合,以及如何将集合转换为列表。我们学习了如何创建浅拷贝以及如何遍历可迭代对象的浅拷贝(列表)。

更新于: 2022-10-27

2K+ 阅读量

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.