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.
这篇关于找到多组交集的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:找到多组交集的最佳方法?


- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01