python获得两个数组的交集、并集、差集

2015-11-12 0 997
python获得两个数组的交集、并集、差集

python获得两个数组的交集、并集、差集 1. 获取两个list 的交集

#方法一:
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)))


2. 获取两个list 的并集

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


3. 获取两个 list 的差集

print list(set(b).difference(set(a))) # b中有而a中没有的


通过以上方法,就能处理python list 的交集,并集,差集了。 

遇见资源网 python python获得两个数组的交集、并集、差集 http://www.ox520.com/15928.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务