How to generate all permutations of a string?

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Thu Jun 22 04:47:50 EDT 2006


In article <mailman.7352.1150963651.27775.python-list at python.org>,
 "Girish Sahani" <girish at cse.iitb.ac.in> wrote:

>  I want to generate all permutations of a string.

def permute(Seq) :
    """generator which yields successive permutations of the elements
    of Seq."""
    if len(Seq) == 0 :
        yield ()
    else :
        for i in range(0, len(Seq)) :
            for rest in permute(Seq[:i] + Seq[i + 1:]) :
                yield (Seq[i],) + rest
            #end for
        #end for
    #end if
#end permute



More information about the Python-list mailing list