1.python List交集、并集、差集
1). 擷取兩個list 的交集
#方法一:
a=[2,3,4,5]
b=[2,5,8]
tmp = [val for val in a if val in b]
print tmp
#[2, 5]
#方法二
print list(set(a).intersection(set(b)))
2). 擷取兩個list 的并集
print list(set(a).union(set(b)))
3). 擷取兩個 list 的差集
print list(set(b).difference(set(a))) # b中有而a中沒有的
2.python Set交集、并集、差集
s = set([3,5,9,10,20,40]) #建立一個數值集合
t = set([3,5,9,1,7,29,81]) #建立一個數值集合
a = t | s # t 和 s的并集 ,等價于t.union(s)