如何在python中从字符串中删除重复记录?我的方法都被拒绝了。下面是我的代码

如何在python中从字符串中删除重复记录?我的方法都被拒绝了。下面是我的代码,python,Python,这是我一直坚持的代码,任何人都可以指出这里出了什么问题,或者给我一个实现这一点的替代方法 def remove_duplicates(string): s = string.split() return string.replace(s[0],"") def RemoveDupliChar(Word): NewWord = " " index = 0 for char in Word: if ch

这是我一直坚持的代码,任何人都可以指出这里出了什么问题,或者给我一个实现这一点的替代方法

def remove_duplicates(string):

    s = string.split()
    return string.replace(s[0],"")

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

如果可能,请尝试添加至少一个示例。 这有助于理解问题

根据我对你的问题的理解,这里有一个可能的例子来说明你的解决方案

>>> def func1(str1):
...     strarr1 = str1.split()
...     tempstr = []
...     for i in strarr1:
...             if i not in tempstr:
...                     tempstr.append(i);
...     print (tempstr)
...
示例:输入:func1he he hello bo bolo hee hee he

输出:['he'、'hello'、'bolo'、'bo'、'hee']

或者这就是你想要的:

>>> def func2(str1):
...     str2arr=[]
...     str3arr=[]
...     str4 = ''
...     for i in str1:
...             if i not in str2arr:
...                     str2arr.append(i)
...             else:
...                     str3arr.append(i)
...     print (str2arr)
...
示例输入:func2heeelo

输出:['h','e','l','o']

我想这可能会有帮助

def removeDupWords(list):
    newList = []
    for x in list:
        if x not in newList:
            newList.append(x)
    return newList

s="Hello World World Hello"
output=' '.join(removeDupWords(s.split(' ')))
print(output)
输出:你好,世界

输出:

[aehloruwy',5]


可能已看到相同的错误消息。此错误消息是什么?您已在没有任何详细信息的情况下转储了代码。如果我正确理解了您的目的,建议复制的顶部答案肯定会起作用。函数应该删除字符串中所有重复的字符,并返回一个包含两个值的元组:一个只包含唯一排序字符的新字符串。删除的重复项总数。函数应删除字符串中的所有重复字符,并返回一个包含两个值的元组:一个仅包含唯一排序字符的新字符串。删除的重复项的总数。例如:remove_duplicates'aaabbbac'=>'abc',5 remove_duplicates'a'=>'a',0 remove_duplicates'thelexash'=>'aehlstx',2该函数应删除字符串中的所有重复字符,并返回一个具有两个值的元组:一个仅包含唯一排序字符的新字符串。删除的重复项的总数。例如:删除重复项'aaabbbac'=>'abc',5删除重复项'a'=>'a',0删除重复项'thelexash'=>'aehlstx',2非常感谢您的欢迎。如果您发现此答案有用,请将其标记为有用。
def removeDupWords(list):
    newList = []
    tuple = []
    duplicates = 0
    for x in list:
        if x not in newList:
            newList.append(x)
        else:
            duplicates += 1
    uniqueString = ''.join(sorted(newList))
    tuple.append(uniqueString)
    tuple.append(duplicates)
    return tuple

s="hellohowareyou"
output=removeDupWords(s)
print(output)