Linux 拨号vps windows公众号手机端

python求并集的方法是什么

lewis 8年前 (2017-05-17) 阅读数 6 #程序编程
文章标签 python

在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()方法,都能得到两个集合的并集。

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门