[Python-Dev] Additional behaviour for itertools.combinations

Raymond Hettinger python at rcn.com
Sun Jan 25 04:33:37 CET 2009


> Raymond Hettinger wrote:
>> 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))

[Nick Coglan] 
> Perhaps a reasonable starting point would be to include this as one of
> the example itertools recipes in the documentation?

I would have suggested that but recipe itself is use case challenged.
The OP did not mention any compelling use cases or motivations.
Essentially, he just pointed-out that it is possible, not that it is desirable.

I can't the of a case where I've wanted to loop over variable length
subsequences.  Having for-loops with tuple unpacking won't work
because the combos have more than one possible size.

This seems like a hypergeneralization to me. 


Raymond 


More information about the Python-Dev mailing list