Creating combination of sequences

Mike C. Fletcher mcfletch at
Sat Nov 13 20:01:14 CET 2004

Minho Chae wrote:

>Hello, python lovers!!
>I'm trying to create combinations of sequences.
A simple recursive generator should produce what you want while being 
fairly easy to read...

 >>> def permute( choices, length=8 ):
...     if length > 1:
...         for suffix in permute(choices,length-1):
...             for choice in choices:
...                 yield choice + suffix
...     else:
...         for choice in choices:
...             yield choice

That's not the same ordering you wanted, but that's merely a matter of 
swapping the order of choice and suffix.


  Mike C. Fletcher
  Designer, VR Plumber, Coder

More information about the Python-list mailing list