Python: Best Way to remove duplicate character from string

后端 未结 7 924
借酒劲吻你
借酒劲吻你 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    
    

提交回复
热议问题