[英]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:我们得到了想要的结果——只删除了第
a
和c
:
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.