Remove duplicate letters in a word

Roman Suzi rnd at onego.ru
Sun Jun 22 14:30:51 EDT 2003


On Sun, 22 Jun 2003, Steven Taschuk wrote:

>Quoth Eliran Gonen:
>  [...]
>> For example, in the word SECRET, E appears twice, I need to get rid of
>> the second instance of E.


Using newest Python technology it could be even simpler:

import sets

def nodups(s):
  seen = sets.Set()
  return "".join([c for c in s if c not in seen and not seen.add(c)])

print nodups('secret')


In fact, resulting string is always very small: 256 chars maximum
for 8bit encoding and 131072 bytes for Unicode (2 byte). So optimizations
concerned with += of strings aren't that effective.


Sincerely yours, Roman Suzi
-- 
rnd at onego.ru =\= My AI powered by GNU/Linux RedHat 7.3






More information about the Python-list mailing list