美文网首页
python中的交集、并集、差集计算

python中的交集、并集、差集计算

作者: 小甜瓜Melon | 来源:发表于2017-07-19 17:46 被阅读0次

** a和b均为list **

交集

print list(set(a).intersection(set(b)))
等价于 
tmp=[val for val in a if val inb]

并集

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

差集

print list(set(b).difference(set(a)))   #b中包含但a不包含

完。

相关文章

网友评论

      本文标题:python中的交集、并集、差集计算

      本文链接:https://www.haomeiwen.com/subject/hvtrkxtx.html