new itertools functions in Python 2.6

Mark Dickinson dickinsm at gmail.com
Wed Jul 16 06:05:38 EDT 2008


On Jul 16, 7:20 am, Mensanator <mensana... at aol.com> wrote:
> > > ##  Combinations with replacement
> > > ##  -----------------------------
> > > ##  aaa aab aac aad aae abb abc abd abe acc acd ace
> > > ##  add ade aee bbb bbc bbd bbe bcc bcd bce bdd bde
> > > ##  bee ccc ccd cce cdd cde cee ddd dde dee eee
> > > ##
> > > ##  actual words: 35    (m+n-1)!/(n!(m-1)!) words: 35

>>> for x in combinations(range(7), 4):
...     x = [-1] + list(x) + [7]
...     print ''.join(c*(x[i+1]-x[i]-1) for i, c in
enumerate('abcde'))
...
eee
dee
dde
ddd
cee
cde
cdd
cce
ccd
ccc
bee
bde
bdd
bce
bcd
bcc
bbe
bbd
bbc
bbb
aee
ade
add
ace
acd
acc
abe
abd
abc
abb
aae
aad
aac
aab
aaa


Generalization left as an exercise for the reader.

Mark



More information about the Python-list mailing list