社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

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

Marioko53 • 4 年前 • 2140 次点击  

我得到了以下字符串 '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')

结果是 'abcde' 不是我要找的那个 “bcdea”

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130521
文章 [ 5 ]  |  最新文章 4 年前
Joshua Voskamp
Reply   •   1 楼
Joshua Voskamp    5 年前

改变

new_list = ''.join(new_list)

到

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

(并找出原因!提示:你的身体状况如何 if 块你在查什么,为什么?)

Loïc
Reply   •   2 楼
Loïc    5 年前

str.replace() 是否:

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

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

返回包含所有子字符串old的字符串副本 换成了新的。如果给出了可选参数计数,则只有 第一次计数的出现被替换。

所以基本上:

"abcabc".replace('b', '', 1)
# output : 'acabc'
Bhagyesh Dudhediya
Reply   •   3 楼
Bhagyesh Dudhediya    5 年前

其中一种方法是在 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')

输出:

bcdeaa
aabcdea
rv.kvetch
Reply   •   4 楼
rv.kvetch    5 年前

一种方法是在字符串上按相反顺序迭代,并跟踪字符串中看到的所有字符。如果一个字符被重复,我们不会将其添加到 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'))

结果: 'bcdea'


请注意,上述解决方案并不能完全按照预期工作,因为它将删除除最后一个字符外的所有字符;例如,如果一个chracter出现了两次以上,并且只想删除第一次。要解决这个问题,您可以执行以下操作:

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)

现在,对于给定的输入:

print(remove_rep('abcdeaca'))

我们得到了想要的结果——只有第一个 a 和 c 已删除:

bdeaca

测试更复杂的输入:

print(remove_rep('abcdeaabcdea'))

我们确实得到了正确的结果:

aabcdea

你看到最后一个发生了什么吗?第一个 abcde 序列被删除,因为此字符串中的所有字符都是重复的。所以我们的结果实际上是正确的,尽管乍一看并不是这样。

Maurice Meyer
Reply   •   5 楼
Maurice Meyer    5 年前

你可以利用 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')

出:

bcdea
bcdea