集合中的(交集,并集,差集,补集,对称差集)

admin 46 2024-02-12 12:37:14

  Python中求集合的交集、并集、差集和对称集非常简单,具体代码如下所示:

  交集:

  ```python

  set1 = {1, 2, 3}

  set2 = {2, 3, 4}

  intersection_set = set1 & set2

  print(intersection_set)

  ```

  输出结果为:{2, 3}

  并集:

  ```python大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!

  set1 = {1, 2, 3}

  set2 = {2, 3, 4}

  union_set = set1 | set2大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!

  print(union_set)

  ```

  输出结果为:{1, 2, 3, 4}

  差集:

  ```python

  set1 = {1, 2, 3}

  set2 = {2, 3, 4}

  difference_set = set1 - set2

  print(difference_set)

  ```

  输出结果为:{1}

  对称集:

  ```python大佬们都在玩{精选官网网址: www.vip333.Co }值得信任的品牌平台!

  set1 = {1, 2, 3}

  set2 = {2, 3, 4}

  symmetric_difference_set = set1 ^ set2

  print(symmetric_difference_set)

  ```

  输出结果为:{1, 4}

集合中的(交集,并集,差集,补集,对称差集)

集合中的(交集,并集,差集,补集,对称差集)

上一篇:高尔夫
下一篇:中国乒乓球队历届世界冠军一览表
相关文章
返回顶部小火箭