[Python-Dev] Additional behaviour for itertools.combinations

Raymond Hettinger python at rcn.com
Sat Jan 24 21:23:51 CET 2009


From: "Konrad Delong" <konryd at gmail.com>
> I'm not sure if it's the right place to post it. If so - I'll be glad
> to learn where is one.

Please post a feature request on the bug tracker and assign it to me.

> Anyway:
> I think the function itertools.combinations would benefit from making
> the 'r' (length of the combinations) argument optionally a sequence.
> 
> With that change one could call combinations(sequence, [2, 3]) in
> order to get all combinations of length 2 and 3.
> In particular, one could call combinations(sequence,
> range(len(sequence)) in order to get *all* combinations of given
> sequence.

This design is similar to the API for similar functionality in mathematica.

The question is whether there are sufficient worthwhile use cases
to warrant the added API complexity and algorithm complexity.

The latter is a bit tricky if we want to maintain the lexicographic
ordering and the notion of combinations being a subsequence of
the permutations code.

Since I expect  students to be among the users for the comb/perm 
functions, there is some merit to keeping the API as simple as possible.
Besides, it is not hard to use the existing tool as a primitive to get to
the one you want:

   def mycombinations(iterable, r_seq):
         # mycombinations('abc', [1,2]) --> A B C AB AC BC
         iterable = list(iterable)
         return chain.from_iterable(imap(combinations, repeat(iterable), r_seq))
    


> PS. Didn't want to spoil the beginning of the post, but I consider it
> to be a good practice to introduce oneself when posting the first
> time, so: Hello, my name is Konrad, I'm an IT student and I'm
> following python-dev for some time, but never posted before.

Hello Konrad.  Welcome to python-dev.



Raymond Hettinger


More information about the Python-Dev mailing list