set集合是一個無序且不重複的元素集合
1.set的創建
2種方式:
se = {11,22,33}
se = set([11,22,33]) #呼叫的set的__init__方法進行建立
2.set的常用方法
1.add
se = {11,22,33}se.add(44)
print(se) => { 33,11,44,22} #因為是無序的,所以執行的結果會不一樣,但是44確實被增加到了原set集合中
2.remove
se = {11,22,33}
se.remove(11)
print (se) => {22,33}
se.remove(44) #報錯,提示找不到指定的元素
3.discard
se = {11,222222222222222222222222222222號,33}se.discard(11)
print (se) => {22,33}se.discard(44)
print (se) => {11,22,33} #找不到指定的元素時,不刪除,不報錯
4.pop
se = {11,22,33}
se.pop()print (se) => {11,22} #隨機出一個元素,執行結果可能不堆疊相同ret = se.pop()print (ret) => {33} #將出棧的結果印出來
5.difference
se1 = {11, 22, 33, 44}
se1 = {11, 22, 33, 44}
se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22,se2 = {22. 33, 44, 55}
6.difference_update
se1 = {11,22,33,44}
se2 = {22,33,44,55}
7.intersection
se1 = {11,22,33,44}
se2 = {22,33, 44,55}
8.intersection_update
se1 = {111,22,3334442 = {22,33,44,55}
se1.intersection_update(se2)
print (se1) => {33,44,22} #將se1,se2的交集覆寫至se1的集合中
se1 = {11,22,33,44}
se2 = {22,33,44,55}
print (se1.union(se2)) => {11,22,33,44,55} # se1,se2的並集