Python:从字符串中删除重复字符的最佳方法

时间:2013-09-14 06:30:22

标签: python string text-processing

如何使用Python从字符串中删除重复的字符?例如,假设我有一个字符串:

foo = "SSYYNNOOPPSSIISS"

如何制作字符串:

foo = SYNOPSIS

我是python的新手,我已经厌倦了并且它正在工作。我知道有聪明的方法可以做到这一点......只有经验可以证明这一点..

def RemoveDupliChar(Word):
        NewWord = " "
        index = 0
        for char in Word:
                if char != NewWord[index]:
                        NewWord += char
                        index += 1
        print(NewWord.strip()) 

注意:订单很重要,此问题与this一个不相似。

7 个答案:

答案 0 :(得分:19)

使用itertools.groupby

>>> foo = "SSYYNNOOPPSSIISS"
>>> import itertools
>>> ''.join(ch for ch, _ in itertools.groupby(foo))
'SYNOPSIS'

答案 1 :(得分:4)

这是一个不导入itertools的解决方案:

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

Out[1]: 'SYNOPSIS'

但它比其他方法慢!

答案 2 :(得分:2)

这个怎么样:

oldstring = 'SSSYYYNNNOOOOOPPPSSSIIISSS'
newstring = oldstring[0]
for char in oldstring[1:]:
    if char != newstring[-1]:
        newstring += char    

答案 3 :(得分:1)

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")

当我们将char与列表的最后一个元素进行比较时,我发现使用itertools和list comprehesion的解决方案甚至是解决方案

答案 4 :(得分:0)

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)  

答案 5 :(得分:0)

怎么样

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)

答案 6 :(得分:-1)

您可以尝试以下方法:

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))
相关问题