Pulling all n-sized combinations from a list

Jack Diederich jack at performancedrivers.com
Thu Feb 9 11:06:01 EST 2006


On Wed, Feb 08, 2006 at 12:50:19PM -0800, Swroteb wrote:
> Hi there,
> 
> I've got a reasonably sized list of objects that I'd like to pull out
> all combinations of five elements from.  Right now I have a way to do
> this that's quite slow, but manageable.  I know there must be a better
> way to do this, but I'm not sure what it is.  Here's what I've got so
> far:
> 

import probstat # http://probstat.sourceforge.net
for (a, b, c) in probstat.Combination(range(9), 3): # 0..9, pick 3
  print a, b, c

It is a C extension that does permutations & combinations and is
about 10x faster than doing it in pure python [I'm the author].
It is also the 3rd result for "python combination" and 5th for
"python permutaiton" but every month someone posts to c.l.py asking
for something similar.  Go figure.

-jackdied



More information about the Python-list mailing list