2014/01/24

Python 3.3 Sets

Set意思是集合,一個集合內每個值都是唯一,空集合也是一種集合


#Set
s = {1, 5, 7, 8, 9, 1}
print("定義s為一個集合:", s)

#s且不再n集合內
print("s且不再n集合內:", s - {1})

#不再s和n裡的集合
print("不再s和n裡的集合", s ^ {1, 5, 7, 8, 9, 10, 11.1})

#交集
print("交集:", s & {1})

#聯集
print("聯集:", s | {10})

集合有聯集以及交集,以上面的例子,以文氏圖解說:


聯集:s or n





交集:s and n


s且不再n集合內:s and ~n

不再s和n裡的集合:~(s and n)




參考資料:
http://docs.python.org/3/tutorial/datastructures.html#more-on-lists