finding the intersection of a list of Sets

Peter Otten __peter__ at web.de
Tue Jan 31 05:40:01 EST 2006


Suresh Jeevanandam wrote:

> I have a list of sets in variable lsets .
> Now I want to find the intersection of all the sets.
> 
> r = lsets[0]
> for s in r[0:]:
>     r = r & s

Try to post working examples.

> Is there any other shorter way?

>>> sets = map(set, "abc bcd cde".split())
>>> reduce(set.intersection, sets)
set(['c'])

Peter



More information about the Python-list mailing list