Best way to find the intersection of multiple sets?(找到多组交集的最佳方法?)
问题描述
我有一个集合列表:
setlist = [s1,s2,s3...]
我想要 s1 ∩ s2 ∩ s3 ...
I want s1 ∩ s2 ∩ s3 ...
我可以编写一个函数来执行一系列成对的s1.intersection(s2)等
I can write a function to do it by performing a series of pairwise s1.intersection(s2), etc.
有推荐的、更好的或内置的方法吗?
Is there a recommended, better, or built-in way?
推荐答案
从 Python 2.6 版开始,您可以使用多个参数来 set.intersection(),如
From Python version 2.6 on you can use multiple arguments to set.intersection(), like
u = set.intersection(s1, s2, s3)
如果集合在列表中,则转换为:
If the sets are in a list, this translates to:
u = set.intersection(*setlist)
其中 *a_list 是 列表扩展
请注意,set.intersection 不是静态方法,但它使用函数符号来应用第一个集合与列表其余部分的交集.因此,如果参数列表为空,这将失败.
Note that set.intersection is not a static method, but this uses the functional notation to apply intersection of the first set with the rest of the list. So if the argument list is empty this will fail.
这篇关于找到多组交集的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找到多组交集的最佳方法?
基础教程推荐
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
