fastest way to find the intersection of n lists of sets

Alex Martelli aleax at mac.com
Mon Apr 30 03:41:33 CEST 2007


Prateek <surekap at gmail.com> wrote:

> >      For the above example, it's worth sorting lists_of_sets by the
> > length of the sets, and doing the short ones first.
> 
> Thanks. I thought so - I'm doing just that using a simple Decorate-
> Sort-Undecorate idiom.

Use, instead, the DSU that is now build into Python:

listofsets.sort(key=len)

this will be faster (as well as more readable &c) than programming your
own DSU, and is exactly the reason the key= parameter was added.

I also suggest avoiding reduce in favor of a simple explicit loop.


Alex



More information about the Python-list mailing list