找到多个集合的交集的最佳方法? [英] 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?
推荐答案
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屋!
查看全文