Pulling all n-sized combinations from a list

Swroteb swrittenb at gmail.com
Wed Feb 8 17:07:13 EST 2006


Paul Rubin wrote:
> "Swroteb" <swrittenb at gmail.com> writes:
> > Atrocious and slow, I'm sure, but is there a better way?  I can't
> > simply create a list with the combinations I want, since it won't fit
> > into memory.  And I'm sure I can do it using a standard paradigm using
> > five indexed for loops (ie, for i = 1, for j = i+1, for k = j+1, etc.).
> >  But is there a really nice way to handle this in Python?
>
> Is this a homework problem?  Hint: 1) use recursion; 2) use generators.

I appreciate the response; no, this is not a homework problem.

I'm a little bit confused about your generator suggestion.  My list is
a set of references to instantiated objects.  I'm just unsure about how
to iterate through every unique combination of those references.  Are
you suggesting that I set up methods to provide the indices I'm looking
for in the list to iterate over?




More information about the Python-list mailing list