集合关系的交集,差,并集的运算
Posted monster7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了集合关系的交集,差,并集的运算相关的知识,希望对你有一定的参考价值。
1.交集
r = ["hehe","haha","woaini"] v = ["hehe","haha","nishishabi"] p = set(r) y = set(v) print(p.intersection(y)) print(p&y) {‘hehe‘, ‘haha‘} {‘hehe‘, ‘haha‘}
用intersection或者&都可以
2.并集
r = ["hehe","haha","woaini"] v = ["hehe","haha","nishishabi"] p = set(r) y = set(v) print(p.union(y)) print(p|y) {‘haha‘, ‘hehe‘, ‘woaini‘, ‘nishishabi‘} {‘haha‘, ‘hehe‘, ‘woaini‘, ‘nishishabi‘}
用union或者|都可以
3.差集
r = ["hehe","haha","woaini"] v = ["hehe","haha","nishishabi"] p = set(r) y = set(v) print(p-y) print(p.difference(y)) {‘woaini‘} {‘woaini‘}
用difference或者-都可以
以上是关于集合关系的交集,差,并集的运算的主要内容,如果未能解决你的问题,请参考以下文章