是的,Set在Python中可以進行集合運算。可以使用以下常見的集合運算:
例如:
set1 = {1, 2, 3}
set2 = {2, 3, 4}
# 并集
union_set = set1.union(set2)
print(union_set) # 輸出 {1, 2, 3, 4}
# 交集
intersection_set = set1.intersection(set2)
print(intersection_set) # 輸出 {2, 3}
# 差集
difference_set = set1.difference(set2)
print(difference_set) # 輸出 {1}
# 對稱差集
symmetric_difference_set = set1.symmetric_difference(set2)
print(symmetric_difference_set) # 輸出 {1, 4}