Python: Best Way to remove duplicate character from string

后端 未结 7 731
借酒劲吻你
借酒劲吻你 2020-12-01 21:46

How can I remove duplicate characters from a string using Python? For example, let\'s say I have a string:

foo = \"SSYYNNOOPPSSIISS\"

How c

相关标签:
7条回答
  • 2020-12-01 21:53

    How about this:

    oldstring = 'SSSYYYNNNOOOOOPPPSSSIIISSS'
    newstring = oldstring[0]
    for char in oldstring[1:]:
        if char != newstring[-1]:
            newstring += char    
    
    0 讨论(0)
  • 2020-12-01 22:04

    How about

    foo = "SSYYNNOOPPSSIISS"
    
    
    def rm_dup(input_str):
        newstring = foo[0]
        for i in xrange(len(input_str)):
            if newstring[(len(newstring) - 1 )] != input_str[i]:
                newstring += input_str[i]
            else:
                pass
        return newstring
    
    print rm_dup(foo)
    
    0 讨论(0)
  • 2020-12-01 22:04

    You can try this:

    string1 = "example1122334455"
    string2 = "hello there"
    
    def duplicate(string):
        temp = ''
    
        for i in string:
            if i not in temp: 
                temp += i
    
        return temp;
    
    print(duplicate(string1))
    print(duplicate(string2))
    
    0 讨论(0)
  • 2020-12-01 22:05

    This is a solution without importing itertools:

    foo = "SSYYNNOOPPSSIISS"
    ''.join([foo[i] for i in range(len(foo)-1) if foo[i+1]!= foo[i]]+[foo[-1]])
    
    Out[1]: 'SYNOPSIS'
    

    But it is slower than the others method!

    0 讨论(0)
  • 2020-12-01 22:07
    def removeDuplicate(s):  
        if (len(s)) < 2:
            return s
    
        result = []
        for i in s:
            if i not in result:
                result.append(i)
    
        return ''.join(result)  
    
    0 讨论(0)
  • 2020-12-01 22:09
    def remove_duplicates(astring):
      if isinstance(astring,str) :
        #the first approach will be to use set so we will convert string to set and then convert back set to string and compare the lenght of the 2
        newstring = astring[0]
        for char in astring[1:]:
            if char not in newstring:
                newstring += char    
        return newstring,len(astring)-len(newstring)
      else:
    raise TypeError("only deal with alpha  strings")
    

    I've discover that solution with itertools and with list comprehesion even the solution when we compare the char to the last element of the list doesn't works

    0 讨论(0)
提交回复
热议问题