两个list取交集、并集、差集

一、交集
python

#方法一:
a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]
print tmp
#[2, 5]

#方法二
print list(set(a).intersection(set(b)))

二、并集code

print list(set(a).union(set(b)))

三、差集io

print list(set(b).difference(set(a))) # b中有而a中没有的
相关文章
相关标签/搜索