zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Python Set intersection() 方法详解编程语言

Pythonset方法编程语言 详解 intersection
2023-06-13 09:11:47 时间
set1 必需,要查找相同元素的集合 set2 可选,其他要查找相同元素的集合,可以多个,多个使用逗号 , 隔开

返回一个新的集合。

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

 x = {"apple", "banana", "cherry"} 

 y = {"google", "runoob", "apple"} 

 z = x.intersection(y) 

 print(z)

输出结果为:

 {apple}

计算多个集合的并集:

实例 1
 x = {"a", "b", "c"} 

 y = {"c", "d", "e"} 

 z = {"f", "g", "c"} 

 result = x.intersection(y, z) 

 print(result)

输出结果为:

 {c}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/17087.html

cgojava