是的,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}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:python中set能进行集合运算吗