[Python-Dev] Additional behaviour for itertools.combinations

Steven D'Aprano steve at pearwood.info
Sun Jan 25 11:49:43 CET 2009


On Sun, 25 Jan 2009 02:33:37 pm Raymond Hettinger wrote:
> > 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.

Does answering homework questions count as a use-case?

http://mathforum.org/library/drmath/view/56121.html

Also calculating the odds of winning Powerball:

http://mathforum.org/library/drmath/view/56122.html

The number of combinations taken (1, 2, 3, ..., n) at a time is closely 
related to the Bell Numbers. And according to Wikipedia, the oldest 
known reference to combinatrics included such a question.

http://en.wikipedia.org/wiki/History_of_combinatorics

Having said all that, I'm inclined to agree that this is an 
over-generalisation. As far as I can tell, there's no name for this in 
mathematics, which suggests that useful applications and theorems are 
both rare.

In any case, it's not that difficult to create a generator to yield all 
the combinations:

(comb for k in ks for comb in itertools.combinations(seq, k))

I'm with Nick that this would make a good example for the documentation. 
I don't object to combinations growing the extra functionality, but if 
it does, people will ask why permutations doesn't as well.



-- 
Steven D'Aprano


More information about the Python-Dev mailing list