1. 程式人生 > >Python 比較兩個數組的元素的異同

Python 比較兩個數組的元素的異同

section pri 異同 blog nio 兩個 set diff int

通過set()獲取兩個數組的交/並/差集:

print set(a).intersection(set(b)) # 交集
print set(a).union(set(b)) # 並集
print set(a).difference(set(b)) # 差集,在a中但不在b中的元素
print set(b).difference(set(a)) # 差集,在b中但不在a中的元素

Python 比較兩個數組的元素的異同