集合 (Set):
set 是一種內建的資料結構,用於儲存唯一且無序的元素集合
特點:
- 唯一性:set 中的元素不會重複,即使你嘗試加入重複的元素,set 也會自動忽略
- 無序性:set 中的元素沒有固定的順序,因此不能通過索引來訪問元素
- 可變性:set 是可變的,可以動態地添加或刪除元素
- 支援集合運算:set 支援交集、聯集、差集等數學集合運算
建立 set
1 2 3 4 5 |
# 注意:如果使用 {} 而不放入任何元素,Python 會將其視為空字典,而不是空 set # 要創建空 set,必須使用 set() empty_set = set() my_set = {1, 2, 3} |
set 相關方法:
add(element)
將單個元素添加到 set 中
如果元素已經存在,則不會有任何變化
1 2 3 4 |
my_set = {1, 2, 3} my_set.add(4) print(my_set) # {1, 2, 3, 4} |
update(iterable)
將一個可迭代對象(如列表、元組、集合等)中的所有元素添加到 set 中
如果元素已經存在,則會被忽略
S |= T
1 2 3 |
my_set = {1, 2, 3} my_set.update([3, 4, 5]) print(my_set) # {1, 2, 3, 4, 5} |
remove(element)
刪除 set 中的指定元素
如果元素不存在,會拋出 KeyError 錯誤
1 2 3 |
my_set = {1, 2, 3} my_set.remove(2) print(my_set) # {1, 3} |
discard(element)
刪除 set 中的指定元素
如果元素不存在,不會拋出錯誤
1 2 3 |
my_set = {1, 2, 3} my_set.discard(2) print(my_set) # {1, 3} |
pop()
隨機刪除並返回 set 中的一個元素
如果 set 為空,會拋出 KeyError 錯誤
1 2 3 |
my_set = {1, 2, 3} element = my_set.pop() print(element) # 可能輸出: 1, 2 或 3 |
clear()
清空 set 中的所有元素
1 2 3 |
my_set = {1, 2, 3} my_set.clear() print(my_set) # set() |
union(*others)
返回當前 set 與其他集合的聯集
等同於使用 | 運算符
S | T
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} union_set = set1.union(set2) print(union_set) # {1, 2, 3, 4, 5} |
intersection(*others)
返回當前 set 與其他集合的交集
等同於使用 & 運算符
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} intersection_set = set1.intersection(set2) print(intersection_set) # {3} |
difference(*others)
返回當前 set 與其他集合的差集
等同於使用 – 運算符
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} difference_set = set1.difference(set2) print(difference_set) # {1, 2} |
symmetric_difference(other)
返回當前 set 與另一個集合的對稱差集(即兩個集合中獨有的元素)
等同於使用 ^ 運算符
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} symmetric_difference_set = set1.symmetric_difference(set2) print(symmetric_difference_set) # {1, 2, 4, 5} |
intersection_update(*others)
更新當前 set,只保留與其他集合的交集
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} set1.intersection_update(set2) print(set1) # {3} |
difference_update(*others)
更新當前 set,移除與其他集合共有的元素
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} set1.difference_update(set2) print(set1) # {1, 2} |
symmetric_difference_update(other)
更新當前 set,只保留與另一個集合的對稱差集
S ^= T
1 2 3 4 |
set1 = {1, 2, 3} set2 = {3, 4, 5} set1.symmetric_difference_update(set2) print(set1) # {1, 2, 4, 5} |
issubset(other)
檢查當前 set 是否是另一個集合的子集
等同於使用 <= 運算符
1 2 3 |
set1 = {1, 2} set2 = {1, 2, 3} print(set1.issubset(set2)) # True |
issuperset(other)
檢查當前 set 是否是另一個集合的超集
等同於使用 >= 運算符
1 2 3 |
set1 = {1, 2, 3} set2 = {1, 2} print(set1.issuperset(set2)) # True |
isdisjoint(other)
檢查當前 set 與另一個集合是否沒有共同元素
1 2 3 |
set1 = {1, 2} set2 = {3, 4} print(set1.isdisjoint(set2)) # True |
set 相關操作:
len()
返回集合中的元素個數
1 2 |
my_set = {1, 2, 3} print(len(my_set)) # 3 |
in 運算符
檢查元素是否存在於集合中
1 2 3 |
my_set = {1, 2, 3} print(2 in my_set) # True print(4 in my_set) # False |
sorted()
返回一個排序後的列表(set 本身無序)
1 2 3 |
my_set = {3, 1, 2} sorted_list = sorted(my_set) print(sorted_list) # [1, 2, 3] |
min()、max() 和 sum()
min(): 返回集合中的最小值
max(): 返回集合中的最大值
sum(): 返回集合中所有元素的總和
1 2 3 4 |
my_set = {1, 2, 3} print(min(my_set)) # 1 print(max(my_set)) # 3 print(sum(my_set)) # 6 |
frozenset
創建一個不可變的集合,可以作為字典的鍵或集合的元素
1 2 3 |
my_frozenset = frozenset([1, 2, 3]) my_dict = {my_frozenset: "value"} print(my_dict) # {frozenset({1, 2, 3}): 'value'} |