Python 删除两个列表之间的公共元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13672543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 09:21:36  来源:igfitidea点击:

Removing the common elements between two lists

python

提问by OneMoreError

Possible Duplicate:
Python list subtraction operation

可能的重复:
Python 列表减法操作

I want to remove the common elements between two lists. I mean something like this

我想删除两个列表之间的公共元素。我的意思是这样的



a=[1,2,3,4,5,6,7,8]
b=[2,4,1]
# I want the result to be like
res=[3,5,6,7,8]


Is there any simple pythonic way to do this ?

有没有简单的pythonic方法来做到这一点?

采纳答案by dugres

use sets :

使用集:

res = list(set(a)^set(b))

回答by dugres

You can use sets learn more from here

您可以使用集合从这里了解更多信息

print(set(a).difference(b))