Python 集合(二)

用書 PYTHON王者歸來 作者洪錦魁

add()加元素到集合
copy() 複製集合
remove() 刪除集合指定元素 (若要刪掉的元素不存在會有錯誤產生)
discard() 刪除集合指定元素 (若要刪掉的元素不存在也不會有錯誤產生)
pop()隨機刪除集合元素
clear() 刪除集合全部元素
isdisjoint() 兩個集合沒共同元素會傳TRUE,否則傳回FALSE
issubset() 測試一個集合是否是另一個集合的子集合
issuperset() 測試一個集合是否是另一個集合的父集合
intersection_update() 傳回集合的交集
update()將一個集的元素呼叫到另一個集合
difference_update() 刪除集合內和另一個集合的相同(重複)元素
symmetric_difference_update() 排除A集合和B集合的共同元素
enumerate()傳回連續整數配對的 enumerate物件
len()元素的數量
max()最大值
min()最小值
sorted()傳回已經排序過的串列,但原本集合不會改變
sum()總和

add()

#add()
A = {"Taiwan","Japan","USA"}
B = {"Ammy","Japan","Lucky","Susan"}
C = {"USA","Taiwan"}
D = (1,2)
A.add("Korea")
print(A)
A.add("Japan")
print(A)
B.add(D)
print(B)

執行結果

copy()

A = {"Taiwan","Japan","USA"}

B = A
B.add("Korea")
print(A)
print(B)
#淺拷貝
C = A.copy()
C.add("U.S")
print(A)
print(C)

執行結果

remove()

#remove()
A = {"Taiwan","Japan","USA"}
print(A)
A.remove("Taiwan")
print(A)
A.remove("Korea")
print(A)

執行結果

discard()

#discard()
A = {"Taiwan","Japan","USA"}
A.discard("Taiwan")
print(A)
A.discard("Korean")
print(A)

print(A.discard("Taiwan"))
print(A.discard("Korean"))

執行結果

pop()

A = {"Taiwan","Japan","USA"}
B= A.pop()
print(A)
print(B)

執行結果

clear()

A = {"Taiwan","Japan","USA"}
A.clear("Taiwan")
print(A)

執行結果

A = {"Taiwan","Japan","USA"}
A.clear()
print(A)
empty_A =set()
print(empty_A)
empty_A.clear()
print(empty_A)

執行結果

isdisjoint()

#isdisjoint() 
A = {"Taiwan","Japan","USA"}
B = {"Ammy","Japan","Lucky","Susan"}
C = {"USA","Taiwan"}
AB =A.isdisjoint(B)
AC =A.isdisjoint(C)
BC =B.isdisjoint(C)
print(AB)
print(AC)
print(BC)

執行結果

issubset()

#isupperset() 
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","USA","Ammy","Lucky","Susan"}
C = {"USA","Taiwan"}
AB =A.issubset(B)
AC =A.issubset(C)
BC =B.issubset(C)
print(AB)
print(AC)
print(BC)

執行結果

issuperset()

#issuperset()
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","USA","Ammy","Lucky","Susan"}
C = {"USA","Taiwan"}

AB =A.issuperset(B)
BA =B.issuperset(A)
BC =B.issuperset(C)
print(AB)
print(BA)
print(BC)

執行結果

intersection_update()

#intersection_update()
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","Ammy","Lucky","Susan"}
C = {"USA","Taiwan","Susan"}

AB =A.intersection_update(B)
print(AB)
print(A)
#AC =A.intersection_update(C)
BC =B.intersection_update(C)
print(BC)
print(B)
CA =C.intersection_update(A)
print(C)

執行結果

update()

#update()
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","Ammy","Lucky","Susan"}
C = {"USA","Taiwan","Susan"}
A.update(B)
print(A)
print(B)
B.update(C)
print(B)
print(C)

執行結果

difference_update()

#differende _update()
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","Ammy","Lucky","Susan"}
C = {"USA","Taiwan","Susan"}
A.difference_update(B)
print(A)
print(B)
B.difference_update(C)
print(B)
print(C)

執行結果

symmetric_difference_update()

#symmetric_difference_update()
A = {"Taiwan","Japan","USA"}
B = {"Taiwan","Japan","Ammy","Lucky","Susan"}
C = {"USA","Taiwan","Susan"}
A.symmetric_difference_update(B)
print(A)
print(B)
B.symmetric_difference_update(C)
print(B)
print(C)

執行結果

凍結集合 frozenset

凍結集合是不可變集合

可用字典的鍵

不可用add() remove()更動凍結集合的內容

#frozenset
x = frozenset(["Taiwan","Japan","USA"])
y = frozenset(["Taiwan","Japan","Korea"])
print(x)
print(y)
print(x&y)
print(x|y)
xy =x & y
xy1 = x | y
xy2 = x.intersection(y)
print(xy)
print(xy1)
print(xy2)

執行結果