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或者-都可以
轉載于:https://www.cnblogs.com/newt/p/8986715.html