首页 > 编程语言 > 详细

Python Set intersection() 方法

时间:2018-11-16 22:11:03      阅读:288      评论:0      收藏:0      [点我收藏+]

描述

intersection() 方法用于返回两个或更多集合中都包含的元素,即交集。

语法

intersection() 方法语法:

1 set.intersection(set1, set2 ... etc)

参数

  • set1 -- 必需,要查找相同元素的集合
  • set2 -- 可选,其他要查找相同元素的集合,可以多个,多个使用逗号 , 隔开

返回值

返回一个新的集合。

实例

返回一个新集合,该集合的元素既包含在集合 x 又包含在集合 y 中:

1 x = {"apple", "banana", "cherry"}
2 y = {"google", "runoob", "apple"}
3  
4 z = x.intersection(y) 
5  
6 print(z)

输出结果为:

1 {apple}

计算多个集合的并集:

实例 1
2 x = {"a", "b", "c"}
3 y = {"c", "d", "e"}
4 z = {"f", "g", "c"}
5  
6 result = x.intersection(y, z)
7  
8 print(result)

输出结果为:

1 {c}

 

Python Set intersection() 方法

原文:https://www.cnblogs.com/lmh001/p/9971748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!