Generating all combinations

Steven D'Aprano steven at REMOVE.THIS.cybersource.com.au
Wed Jun 3 23:48:11 EDT 2009


On Wed, 03 Jun 2009 20:27:56 -0700, Raymond Hettinger wrote:

>> > What, no partitions?
>>
>> >http://en.wikipedia.org/wiki/Partition_of_a_set
> 
> Simpler version:
> 
> def partition(s):
>     n = len(s)
>     parts = range(1, n)
>     for i in range(n):
>         for div in combinations(parts, i):
>             print map(s.__getslice__, chain([0], div), chain(div,
> [n]))



Nice one!


Raymond, as perhaps *the* principle contributor to itertools, do you feel 
that the combinatorics-related tools should be in their own module? Or is 
that dividing the module too fine?

Would you consider moving permutations() and friends into a module 
together with functions for calculating the number of permutations etc? 

e.g. permutations(iterable[, r]) --> permutations object
     nPr(n, r) --> int

and similar.




-- 
Steven



More information about the Python-list mailing list