Python交集有什么作用?
发布时间:2024-08-27 13:47:42

本教程的操作环境:windows7系统,Python 3.9.1,DELL G3电脑。
以属于A和B的元素为元素的集合成为A和B的交集。
1、概念
交集就是要求两个共同的元素集合。
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}
both=my_set1&my_set2
print(both)2、实例
除了通过 & 除符号外,还可以集合 intersection 方法完成。
my_set1={"apple","orange","pear","banana","food"}
my_set2={"apple","orange","pear"}
both=my_set1.intersection(my_set2)
print(both)以上是Python交集的作用介绍,希望对大家有所帮助。更多Python学习指导:python基础教程
下一篇 Python并集是什么意思?
