python 交集

The intersection (A∩B) of two sets A and B is the set that contains all the elements common between both the sets.

交点两组A和B的(A∩B)是集合包含集之间既所有元素常见。

We can perform an intersection between multiple sets too. Following image shows the set intersection example between two sets and three sets.

我们也可以在多个集合之间执行交集。 下图显示了两个集合和三个集合之间的集合相交示例。

Set Intersection

设置相交

Python设置交集 (Python set intersection)

In Python, we can use set class intersection() function to get the intersection of two sets.

在Python中,我们可以使用setintersection()函数来获取两个集合的交集。

Let’s look at an example of the intersection between two sets.

让我们看一下两个集合之间的交集的示例。

set1 = set('abcde')set2 = set('ae')print(set1)
print(set2)# two sets intersection
print(set1.intersection(set2))

Output:

输出:

{'a', 'e', 'b', 'd', 'c'}
{'a', 'e'}
{'a', 'e'}

Python Two Sets Intersection

Python两集相交

The intersection() function takes multiple set arguments too. Let’s look at another example of the intersection between multiple sets.

交集()函数也接受多个设置参数。 让我们看一下多个集合之间交集的另一个示例。

set1 = set('abcde')
set2 = set('ae')
set3 = {'a'}
print(set1.intersection(set2, set3))

Output: {'a'}

输出: {'a'}

Python Multiple Set Intersection

Python多集交集

设置不带参数的交集 (Set intersection without arguments)

We can call intersection() function without an argument too. In this case, a copy of the set will be returned.

我们也可以调用不带参数的交集()函数。 在这种情况下,将返回该集合的副本。

Let’s see if the returned copy refers to the same set or a different one.

让我们看看返回的副本是指相同的集合还是不同的集合。

set1 = set('ab')
set4 = set1.intersection()
print(set4)# check if shallow copy or deep copy
set1.add('0')
set1.remove('a')
print(set1)
print(set4)

Output:

输出:

{'b', 'a'}
{'0', 'b'}
{'c', 'b', 'a'}
GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/24664/python-set-intersection

python 交集

python 交集_Python设置交集相关推荐

  1. python检查_python设置检查点简单实现

    说检查点,其实就是对过去历史的记录,可以认为是log.不过这里进行了简化.举例来说,我现在又一段文本.文本里放有一堆堆的链接地址.我现在的任务是下载那些地址 中的内容.另外因为网络的问题或者网站的问题 ...

  2. python 差异表达_Python设置差异

    python 差异表达 If there are two sets A and B, then the difference between A and B (A-B) is a new set of ...

  3. 站长在线Python精讲:Python中集合的交集、并集、差集和对称差集运算方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<Python中集合的交集.并集.差集和对称差集运算方法详解>.主要讲的是集合运算的相关的概念,及运算方法,包括:集合的交集. ...

  4. LeetCode 757. 设置交集大小至少为2

    LeetCode 757. 设置交集大小至少为2 题目描述 一个整数区间 [a, b] ( a < b ) 代表着从 a 到 b 的所有连续整数,包括 a 和 b. 给你一组整数区间interv ...

  5. 757. 设置交集大小至少为2 : 贪心运用题

    题目描述 这是 LeetCode 上的 757. 设置交集大小至少为2 ,难度为 困难. Tag : 「贪心」 一个整数区间    ( ) 代表着从 a 到 b 的所有连续整数,包括 a 和 b. 给 ...

  6. python excel 单元格格式_python设置单元格数值格式

    python xlwt如何设置单元格格式 python xlwt模块怎么设置excel单元格的属性 如图,默认是general.我想写入的时候就是Text类型.请问应该怎么做. from xlwt i ...

  7. python设置默认utf8编码_Python设置默认编码为utf8的方法

    本文实例讲述了python设置默认编码为utf8的方法.分享给大家供大家参考,具体如下: 这是Python的编码问题,设置python的默认编码为utf8 python安装目录:/etc/python ...

  8. python坐标轴刻度设置_Python Matplotlib 设置x/y坐标轴刻度

    刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(args, *kwargs) 获取或者设置当前刻度位置和文本的 x ...

  9. python权限管理设置_python权限管理框架

    Python生成管理员权限的可执行程序 如何使用pyinstaller或者其他生成一个有管理员权限的可执行程序,生成的我也遇到这个问题了. Python权限修饰正确的是? A. 标识符开头无下划线,该 ...

最新文章

  1. Python可视化中的Matplotlib绘图(1.画图,网格,子图,画正余弦图,坐标轴界限,画圆,)
  2. vim编辑器---批量注释与反注释
  3. Visual Studio 2017 - Update 2预览版已发布
  4. Swift学习笔记-访问控制(Access Control)
  5. JDBC连接SQL Server 2005问题
  6. java.lang.OutOfMemoryError内存溢出定位
  7. 团购的野心,占领网购市场半壁江山?
  8. 计算机毕业设计Java演出票在线预定网站系统(源码+系统+mysql数据库+Lw文档)
  9. mysql数据库异地备份
  10. 《通信软件开发与应用》课程结业报告
  11. WinPE制作相关知识
  12. 计算机组成原理的各种常见英文缩写介绍--<<方便复习的笔记>>
  13. 【转】word公式大括号左对齐
  14. mysql error 1114_ERROR 1114 (HY000): The table is full
  15. ERP实施-有色金属-铜冶炼
  16. ViewPager API
  17. python爬虫: requests爬取flash播放页面的信息
  18. Translation Rule 和命中法则
  19. 简述企业信息系统持续开发与持续集成相关主流技术(DI/CI)
  20. 您的手机上未安装应用程序 / App isn‘t installed

热门文章

  1. 【OpenGL基础篇】——使用面向对象方法封装OpenGL函数(二)
  2. 【转】ACE开发环境搭建
  3. 图片的色彩空间转换、简单色彩跟踪与通道分离、合并(三)
  4. rgbdslam_v2编译过程中引起的needed by错误
  5. angularjs中的单选框绑定数据注意事项
  6. 股灾情形下搞了个满堂红,我也是醉了
  7. NodeJS服务器退出:完成任务,优雅退出
  8. Spring的声明式事务
  9. saltstack安装及简单配置
  10. 自定义ImageButton,实现快进快退功能