>>> a = [1,2,3]
>>> b = [2,4,5]
- 交集
>>> list(set(a).intersection(set(b)))
[2]
- 并集
>>> list(set(a).union(set(b)))
[1, 2, 3, 4, 5]
- 差集
>>> list(set(a).difference(set(b)))
[1, 3]
>>> list(set(b).difference(set(a)))
[4, 5]
>>> a = [1,2,3]
>>> b = [2,4,5]
>>> list(set(a).intersection(set(b)))
[2]
>>> list(set(a).union(set(b)))
[1, 2, 3, 4, 5]
>>> list(set(a).difference(set(b)))
[1, 3]
>>> list(set(b).difference(set(a)))
[4, 5]
本文标题:python 交集、并集、差集
本文链接:https://www.haomeiwen.com/subject/jgcvlctx.html
网友评论