📌  相关文章
📜  从Python中的两个列表中删除公共元素

📅  最后修改于: 2022-05-13 01:54:26.095000             🧑  作者: Mango

从Python中的两个列表中删除公共元素

给定两个列表,任务是编写一个Python程序来删除两个列表的所有公共元素。

例子

方法一:使用Remove()方法

remove() 方法从列表中删除第一个匹配元素(作为参数传递)。

Python3
# Python program to remove common elements
# in the two lists using remove method
def remove_common(a, b):
  
    for i in a[:]:
        if i in b:
            a.remove(i)
            b.remove(i)
  
    print("list1 : ", a)
    print("list2 : ", b)
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)


Python3
# Python program to remove common elements
# in the two lists using list comprehension
def remove_common(a, b):
  
    a, b = [i for i in a if i not in b], 
    [j for j in b if j not in a]
  
    print("list1 : ", a)
    print("list2 : ", b)
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)


Python3
# Python program to remove common elements
# in the two lists using Set’s difference
# operator
def remove_common(a, b):
  
    a, b = list(set(a) - set(b)), 
    list(set(b) - set(a))
  
    print("list1 : ", a)
    print("list2 : ", b)
  
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)


Python3
# Python program to remove common elements
# in the two lists using Set difference()
# method
def remove_common(a, b):
  
    a, b = list(set(a).difference(b)), list(set(b).difference(a))
  
    print("list1 : ", a)
    print("list2 : ", b)
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)


输出:

list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

方法 2:使用列表理解

当您想要基于现有列表的元素创建新列表时,列表推导式提供了更短的语法。

Python3

# Python program to remove common elements
# in the two lists using list comprehension
def remove_common(a, b):
  
    a, b = [i for i in a if i not in b], 
    [j for j in b if j not in a]
  
    print("list1 : ", a)
    print("list2 : ", b)
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)

输出:

list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

方法三:使用Set的差分运算符

差分运算符– 获取第一个集合中的项目,但不获取第二个集合中的项目。

Python3

# Python program to remove common elements
# in the two lists using Set’s difference
# operator
def remove_common(a, b):
  
    a, b = list(set(a) - set(b)), 
    list(set(b) - set(a))
  
    print("list1 : ", a)
    print("list2 : ", b)
  
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)

输出:

list1 :  [1, 2, 3]
list2 :  [6, 7, 8]

方法四:使用Python设置差异()方法

Python中的difference()方法返回一个包含两个集合之间差异的集合,即返回的集合包含仅存在于第一个集合中的项目,并且不包括两个集合中存在的元素。

Python3

# Python program to remove common elements
# in the two lists using Set difference()
# method
def remove_common(a, b):
  
    a, b = list(set(a).difference(b)), list(set(b).difference(a))
  
    print("list1 : ", a)
    print("list2 : ", b)
  
if __name__ == "__main__":
  
    a = [1, 2, 3, 4, 5]
    b = [4, 5, 6, 7, 8]
  
    remove_common(a, b)

输出:

list1 :  [1, 2, 3]
list2 :  [6, 7, 8]