是的,在Python中,set
数据结构可以进行并集操作。你可以使用union()
方法或者|
运算符来实现两个集合的并集。
以下是使用union()
方法的示例:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result) # 输出:{1, 2, 3, 4, 5}
以下是使用|
运算符的示例:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1 | set2
print(result) # 输出:{1, 2, 3, 4, 5}
在这两个示例中,我们首先创建了两个集合set1
和set2
,然后使用union()
方法或|
运算符计算它们的并集,并将结果存储在变量result
中。最后,我们打印出结果。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:python set操作能进行排序吗