找到多组交集的最佳方法? [英] Best way to find the intersection of multiple sets?

查看:23
本文介绍了找到多组交集的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个集合列表:

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.

这篇关于找到多组交集的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆