Re: finding the intersection of a list of Sets

2006-01-31 Thread Raymond Hettinger
That should have been: >>> sets.sort(key=len) >>> reduce(set.intersection, sets) The only refinement was the pre-sort based on set length. -- http://mail.python.org/mailman/listinfo/python-list

Re: finding the intersection of a list of Sets

2006-01-31 Thread Raymond Hettinger
[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']) -- http://mail.python.org/mailman/listinfo/python-list

Re: finding the intersection of a list of Sets

2006-01-31 Thread Peter Otten
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(

finding the intersection of a list of Sets

2006-01-31 Thread Suresh Jeevanandam
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 Is there any other shorter way? Thanks in advance, Suresh -- http://mail.python.org/mailman/listinfo/python-list