0
def remove_duplicates(strng):
 """
 Returns a string which is the same as the argument except only the
 first occurrence of each letter is present. Upper and lower case
 letters are treated as different. Only duplicate letters are removed,
 other characters such as spaces or numbers are not changed. 
 >>> remove_duplicates('apple')
 'aple'
 >>> remove_duplicates('Mississippi')
 'Misp'
 >>> remove_duplicates('The quick brown fox jumps over the lazy dog')
 'The quick brown fx jmps v t lazy dg'
 >>> remove_duplicates('121 balloons 2 u')
 '121 balons 2 u'
 """
 s = strng.split()
 return strng.replace(s[0],"")

Writing a function to get rid of duplicate letters but so far have been playing around for an hour and can't get anything. Help would be appreciated, thanks.

SilentGhost
322k67 gold badges311 silver badges294 bronze badges
asked May 19, 2010 at 11:50
2
  • 2
    This looks like homework. If it is, tag it as such. Commented May 19, 2010 at 11:56
  • If order isn't important to you (but it looks like it is), you can use "".join(set("test")). Commented May 19, 2010 at 20:31

3 Answers 3

3

Not the most efficient, but the most straightforward way is:

>>> s = 'The quick brown fox jumps over the lazy dog'
>>> import string
>>> n = ''
>>> for i in s:
 if i not in string.ascii_letters:
 n += i
 elif i not in n:
 n += i
>>> n
'The quick brown fx jmps v t lazy dg'
Tim Pietzcker
337k59 gold badges519 silver badges572 bronze badges
answered May 19, 2010 at 12:01
0
2

Using a list comprehension :

>>> from string import whitespace, digits
>>> s = 'The quick brown fox jumps over the lazy dog'
>>> ''.join([c for i, c in enumerate(s) if c in whitespace+digits \
 or not c in s[:i]])
answered May 19, 2010 at 20:22
2
  • Nice. I think you should change if c in whitespace+digits to if c not in letters (and thus, from string import letters): your solution would turn "++" into "+" and I don't think that qualifies as a letter. Commented May 19, 2010 at 20:29
  • Why not, the question does not talk about punctuation. In fact, I used this solution instead of string.ascii_letters (as proposed by SilentGhost) to be able to handle non-ascii characters. I think the better would be whitespace+digits+punctuation... but, the question lacks precisions :) Commented May 19, 2010 at 20:40
0

try this ...

def remove_duplicates(s):
 result = ""
 dic = {}
 for i in s:
 if i not in dic:
 result+=i
 if ord(i.lower()) >= ord('a') and ord(i.lower()) <= ord('z'):
 dic[i] = 1
 return result
answered May 19, 2010 at 12:07
6
  • 3
    to check whether value is None, you should use identity check: is None, not equality check (== None). Commented May 19, 2010 at 12:08
  • 1
    Use a set instead of a dict - that is what they are for. Commented May 19, 2010 at 12:10
  • 2
    @SilentGhost, more to the point, use i in dic rather than checking the return value of .get to figure out if something's in a dict. Commented May 19, 2010 at 16:30
  • 1
    @Dave Set does not guarantee that the ordering is maintained. Further, a whitespace will only be counted once; the OP clearly wanted whitespace to be treated differently. Commented May 19, 2010 at 17:55
  • @santa dicts do not guarantee ordering either - this is irrelevant. Using a dict and only testing for the presence or absence of keys is conceptually identical to using a set. Commented May 19, 2010 at 21:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.