String question - find all possible versions of a person's firstname

bonono at gmail.com bonono at gmail.com
Wed Jan 11 07:29:33 EST 2006


Nico Grubert wrote:
> > This sounds like a homework problem.  You might try splitting the name
> > at the e's, check the length of the resulting list and do that many
> > nested loops.
>
> This was my idea too but I am wondering if there are any scripts for
> tasks like this.
>
> Nico

def combine_lol(seq): return reduce(lambda x,y: (a+(b,) for a in x for
b in y), seq, [()])

def shuffle(seq, elem):
    """
    "hello","eo" -> helle,hello,hollo,hollo
    """
    idx = [i for (i,c) in enumerate(seq) if c in elem]
    l = 2**len(idx)
    com = combine_lol([elem]*len(idx))
    pos = ( zip(*x) for x in izip([idx]*l, com) )
    def replace(s, ki):
        for i,e in ki: s[i] = e
        return s

    r = imap(replace, (list(seq) for x in xrange(l)), pos)
    if isinstance(seq,basestring): return ( ''.join(x) for x in r)
    else: return r




More information about the Python-list mailing list