简体   繁体   English

如何使用python删除重复字符的第一次出现

[英]How to remove the first occurence of a repeated Character with python

I have been given the following string 'abcdea' and I need to find the repeated character but remove the first one so the result most be 'bcdea' I have tried to following but only get this result我得到了以下字符串'abcdea' ,我需要找到重复的字符,但删除第一个字符,因此结果大部分是'bcdea'我曾尝试遵循但只能得到这个结果

def remove_rep(x):
    new_list = []
    for i in x:
        if i not in new_list:
            new_list.append(i)
    new_list = ''.join(new_list)
    print(new_list)

remove_rep('abcdea')

and the result is 'abcde' not the one that I was looking 'bcdea'结果是'abcde'而不是我正在寻找的'bcdea'

One approach can be to iterate in reverse order over the string, and keep track of all the characters seen in the string.一种方法可以是在字符串上以相反的顺序迭代,并跟踪字符串中看到的所有字符。 If a character is repeated, we don't add it to the new_list .如果一个字符重复,我们不会将它添加到new_list

def remove_rep(x: str):
    new_list = []
    seen = set()

    for char in reversed(x):
        if char not in seen:
            new_list.append(char)
        seen.add(char)

    return ''.join(reversed(new_list))

print(remove_rep('abcdea'))

Result: 'bcdea'结果: 'bcdea'


Note that the above solution doesn't exactly work as desired, as it'll remove all occurrences of a character except the last one;请注意,上述解决方案并不完全按预期工作,因为它会删除除最后一个字符之外的所有字符; for example, if you have 2+ occurrences of a chracter and you only want to remove the first one.例如,如果一个字符出现了 2 次以上,而您只想删除第一个。 To resolve that, you can instead do something like below:要解决这个问题,您可以改为执行以下操作:

def remove_rep(x: str):
    new_list = []
    first_seen = set()

    for char in x:
        freq = x.count(char)
        if char in first_seen or freq == 1:
            new_list.append(char)
        elif freq > 1:
            first_seen.add(char)

    return ''.join(new_list)

Now for the given input:现在对于给定的输入:

print(remove_rep('abcdeaca'))

We get the desired result - only the first a and c is removed:我们得到了想要的结果——只删除了第ac

bdeaca

Test for a more complicated input:测试更复杂的输入:

print(remove_rep('abcdeaabcdea'))

We do get the correct result:我们确实得到了正确的结果:

aabcdea

Do you see what happened in that last one?你看到最后一个发生了什么吗? The first abcde sequence got removed, as all characters are repeated in this string.第一个abcde序列被删除,因为在这个字符串中所有字符都重复了。 So our result is actually correct, even though it doesn't look so at an initial glance.所以我们的结果实际上是正确的,尽管乍一看并不如此。

You could make use of str.find() , which returns the first occurrence with the string:您可以使用str.find() ,它返回第一次出现的字符串:

def remove_rep(oldString):
    newString = ''
    for i in oldString:
        if i in newString:
            # Character used previously, .find() returns the first position within string
            first_position_index = newString.find(i)
            newString = newString[:first_position_index] + newString[
                first_position_index + 1:]
        newString += i

    print(newString)


remove_rep('abcdea')
remove_rep('abcdeaabcdea')

Out:出去:

bcdea
bcdea

One of the approaches with one small change in the if condition:if条件中有一个小的变化的方法之一:

def remove_rep(x):
    new_list = []
    visited = []
    for i, item in enumerate(x):
        if item not in x[i+1:] or item in visited:
            new_list.append(item)
        else:
            visited.append(item)
    new_list = ''.join(new_list)
    print(new_list)

remove_rep('abcdeaa')
remove_rep('abcdeaabcdea')

Output:输出:

bcdeaa
aabcdea

str.replace() does that : str.replace()这样做:

https://docs.python.org/3/library/stdtypes.html#str.replace https://docs.python.org/3/library/stdtypes.html#str.replace

str.replace(old, new[, count]) str.replace(旧的,新的[,计数])

Return a copy of the string with all occurrences of substring old replaced by new.返回字符串的副本,其中所有出现的子字符串 old 都被 new 替换。 If the optional argument count is given, only the first count occurrences are replaced.如果给出了可选参数计数,则仅替换第一个计数出现。

So basically :所以基本上:

"abcabc".replace('b', '', 1)
# output : 'acabc'

Change改变

new_list = ''.join(new_list)

to

new_list = ''.join(new_list[1:]+[i])

(and figure out why! Hint: what's the condition of your if block? What are you checking for and why?) (并找出原因!提示:你的if块的条件是什么?你在检查什么,为什么?)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保持第一次出现的值在python的数据帧中重复出现? - How to keep the first occurence of a value which is repeated in a dataframe in python? 编写python代码,将字符串中所有重复的字符更改为“ @”,但该字符的首次出现除外 - Write python code that changes all the repeated characters in string to “@” except the first occurence of that character 如何删除列表中第一次出现的整数 - how to remove the first occurence of an integer in a list 如果 python 中的第一个字符是逗号,如何删除 - How to remove first character if it is comma in python 我想在第一次出现时用一个字符分割一个字符串,该字符属于一个字符列表。 如何在python中做到这一点? - I want to split a string by a character on its first occurence, which belongs to a list of characters. How to do this in python? 有没有一种方法可以在 python 中使用 lambda 函数删除第一次出现 - is there an method to remove first occurence using lambda function in python 如何在Dataframe中最后一次出现字符后删除所有内容? - How to remove everything after the last occurence of a character in a Dataframe? 如何删除列表中字符串的第一个字符? [PYTHON] - How to remove the first character of a string that's in a list? [PYTHON] 如何在 python 中打印重复单词的不同字符? - How to print different character of repeated words in python? 使用 python 和正则表达式删除单词中的重复字符串 - Using python and regex to remove repeated character strings within a word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM