How to remove duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • siebertp
    New Member
    • May 2010
    • 3

    How to remove duplicates

    Hi

    I am unable to get the following code to work:
    def remove_duplicat es (strng):
    """
    >>> remove_duplicat es ('apple')
    'aple'
    >>> remove_duplicat es ('Mississippi')
    'Misp'
    >>> remove_duplicat es ('The quick brown fox jumps over the lazy dog')
    'The quick brown fx jmps v t lazy dg'
    >>> remove_duplicat es ('121 balloons 2 u')
    '121 balons 2 u'
    """
    I am required to return 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 and only duplicate letters are removed, other characters such as spaces or numbers are not changed.

    It has to pass in a doctest.

    I cannot get this to work, please can someone help!
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    Shouldn't this be
    >>> remove_duplicat es ('121 balloons 2 u')
    '12 balons 2 u'

    Use a list to store each letter found. Parse the string one letter at a time. If the letter is not found in the list, then it is the first occurrence so you can append it to the list and add it to the re-formated string or list. If the letter is found in the list, do nothing.

    Comment

    • siebertp
      New Member
      • May 2010
      • 3

      #3
      Hi thanks for the reply but the last one needs to be '121 balons 2 u' as the script doesn't delete duplicate numbers. This is where I am getting stuck.

      Comment

      • Glenton
        Recognized Expert Contributor
        • Nov 2008
        • 391

        #4
        Hi please can you post the code you've written so far (please use code tags or it's hard to read), and the problems you're getting with it.

        Is your string always going to be just alphanumeric. And if not, is it just repeated letters that need removing?

        Anyway, this interactive session should solve it for you:
        Code:
        In [6]: sfinal=""
        
        In [7]: s
        Out[7]: 'asdfabwerawef'
        
        In [8]: for c in s:
           ...:     if c not in sfinal:
           ...:         sfinal+=c
           ...:         
           ...:         
        
        In [9]: sfinal
        Out[9]: 'asdfbwer'
        
        In [10]: s="121 balloons 2 u"
        
        In [11]: for c in s:
           ....:     if c.lower() in "abcdefhijklmnopqrstuvwxyz":
           ....:         if c not in sfinal:
           ....:             sfinal+=c
           ....:     else:
           ....:         sfinal+=c
           ....:         
           ....:         
        
        In [12]: sfinal
        Out[12]: 'asdfbwer121 lon 2 u'

        Comment

        • Nonemsludo
          New Member
          • Apr 2010
          • 15

          #5
          take each string or each elem in the string, list it and subject it to the set function and rejoin it
          Code:
          for x in '12 balons 2 u':
                         s=list(x)
                         r=set(s)
                         j=s.join()

          Comment

          • siebertp
            New Member
            • May 2010
            • 3

            #6
            Hi thanks for all the help!

            I used the following code:
            Code:
                result = ""
                for char in strng:
                    if not char in result or not char.isalpha():
                        result += char
                return result
            This returns the result without removing duplicate whitespaces and numerical expressions. Thanks!
            Last edited by bvdet; May 22 '10, 04:13 PM. Reason: Add code tags

            Comment

            Working...