# method one:
a=[2,3,4,5]
b=[2,5,8]
tmp =[val for val in a if val in b]print(tmp)
#[2,5]
# Method two is much faster than method one!
print list(set(a).intersection(set(b)))
# method one:print(list(set(a+b)))
# Method two is much faster than method one!
print(list(set(a).union(set(b))))
# method one:
tmp =[val for val in b if val not in a] #what is in b but not in a
print(tmp)
# Method two is much faster than method one!
print list(set(b).difference(set(a))) #What is in b but not in a is very efficient!
s =set([3,5,9,10,20,40]) #Create a set of values
t =set([3,5,9,1,7,29,81]) #Create a set of values
a = t | s #union of t and s,Equivalent to t.union(s)
b = t & s #the intersection of t and s,Equivalent to t.intersection(s)
c = t - s #Difference set (items are in t but not in s),Equivalent to t.difference(s)
d = t ^ s #Symmetric difference set (items are in t or s, but will not appear in both),Equivalent to t.symmetric_difference(s)
Reference: https://www.cnblogs.com/jlf0103/p/8882896.html
https://www.cnblogs.com/jingtyu/p/7238743.html
Recommended Posts