finding the intersection of a list of Sets

Raymond Hettinger python at rcn.com
Tue Jan 31 11:31:05 EST 2006


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

Very nice.  Here's a refinement:

>>> sets.sort(key=len)
>>> reduce(set.intersection, sets[1:], sets[0])
set(['c'])




More information about the Python-list mailing list