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