我正在尝试创建一个函数,该函数将在给定字符串时删除重复字符的第一次出现。但是有一个细节让我绊倒:不连续的字符不会重复。这样,'bookkeeper'
就可以了'bokeper'
,但是我的功能却退出了'bokepr'
。我怎样才能解决这个问题?这是我的功能。
def remove_repeat(message):
placeholder = [] #The spot that our answer will be built up in
seen_chars = [] #The place that will hold our seen characters
for c in message:
if c not in seen_chars:
seen_chars.append(c)
if c not in placeholder:
placeholder.append(c)
result = ""
for c in placeholder:
result = result + c
return result
def remove_repeat(message):
result = []
curr = None
for c in message:
if c != curr:
curr = c
result.append(c)
return "".join(result)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句