Remove duplicate letters in a word

Anton Vredegoor anton at vredegoor.doge.nl
Sun Jun 22 18:05:53 EDT 2003


Roman Suzi <rnd at onego.ru> wrote:

>
>One more interesting method to do the same:
>
>import sets
>def nodups2(s):
>  l = map(lambda x: (s.index(x[1]),x[0]), enumerate(sets.Set(s)))
>  l.sort()
>  return "".join(map(lambda x: s[x[0]], l))
>
>print nodups2('secret')
>
>
>This implementation uses the fact that index gives the first 
>encounter of a symbol in a string.

How about:

def nodups(s):
    return ''.join([x for i,x in enumerate(s) if s.index(x)==i])

Anton






More information about the Python-list mailing list