Python Set intersection() 方法在数据分析的应用

1、首先进行方法介绍

1.1、描述

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

1.2、语法

intersection() 方法语法:

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

1.3、参数

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

1.4、返回值

返回一个新的集合。

1.5、实例

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

实例 1

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'}

2、Python Set intersection() 方法在数据分析的应用

在这里插入图片描述

发布了123 篇原创文章 · 获赞 54 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_41940950/article/details/97024306
今日推荐