python求并集的方法是什么

1082
2023/12/13 21:39:44
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在Python中,可以使用以下方法来求两个集合的并集:

  1. 使用|运算符:可以通过将两个集合用|运算符连接起来,得到它们的并集。例如:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1 | set2
print(union_set)  # 输出:{1, 2, 3, 4, 5}
  1. 使用union()方法:union()方法可以将一个集合与另一个集合求并集。例如:
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print(union_set)  # 输出:{1, 2, 3, 4, 5}

无论是使用|运算符还是union()方法,都能得到两个集合的并集。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: 如何根据团队技能选择golang爬虫或python爬虫