查找字符串中子字符串的最后一次出现,将其替换

问题:查找字符串中子字符串的最后一次出现,将其替换

因此,我有一长串具有相同格式的字符串,并且我想找到最后一个“”。每个字符,然后将其替换为“。-”。我尝试使用rfind,但似乎无法正确利用它来执行此操作。

So I have a long list of strings in the same format, and I want to find the last “.” character in each one, and replace it with “. – “. I’ve tried using rfind, but I can’t seem to utilize it properly to do this.


回答 0

这应该做

old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]

This should do it

old_string = "this is going to have a full stop. some written sstuff!"
k = old_string.rfind(".")
new_string = old_string[:k] + ". - " + old_string[k+1:]

回答 1

要从右侧替换:

def replace_right(source, target, replacement, replacements=None):
    return replacement.join(source.rsplit(target, replacements))

正在使用:

>>> replace_right("asd.asd.asd.", ".", ". -", 1)
'asd.asd.asd. -'

To replace from the right:

def replace_right(source, target, replacement, replacements=None):
    return replacement.join(source.rsplit(target, replacements))

In use:

>>> replace_right("asd.asd.asd.", ".", ". -", 1)
'asd.asd.asd. -'

回答 2

我会使用正则表达式:

import re
new_list = [re.sub(r"\.(?=[^.]*$)", r". - ", s) for s in old_list]

I would use a regex:

import re
new_list = [re.sub(r"\.(?=[^.]*$)", r". - ", s) for s in old_list]

回答 3

一行代码是:

str=str[::-1].replace(".",".-",1)[::-1]

A one liner would be :

str=str[::-1].replace(".",".-",1)[::-1]


回答 4

您可以使用下面的函数来代替从右至右的单词的第一个出现。

def replace_from_right(text: str, original_text: str, new_text: str) -> str:
    """ Replace first occurrence of original_text by new_text. """
    return text[::-1].replace(original_text[::-1], new_text[::-1], 1)[::-1]

You can use the function below which replaces the first occurrence of the word from right.

def replace_from_right(text: str, original_text: str, new_text: str) -> str:
    """ Replace first occurrence of original_text by new_text. """
    return text[::-1].replace(original_text[::-1], new_text[::-1], 1)[::-1]

回答 5

a = "A long string with a . in the middle ending with ."

#如果要查找任何字符串的最后一次出现的索引,在我们的示例中,我们#将查找with的最后一次出现的索引

index = a.rfind("with") 

#结果将是44,因为索引从0开始。

a = "A long string with a . in the middle ending with ."

# if you want to find the index of the last occurrence of any string, In our case we #will find the index of the last occurrence of with

index = a.rfind("with") 

# the result will be 44, as index starts from 0.


回答 6

天真的方法:

a = "A long string with a . in the middle ending with ."
fchar = '.'
rchar = '. -'
a[::-1].replace(fchar, rchar[::-1], 1)[::-1]

Out[2]: 'A long string with a . in the middle ending with . -'

Aditya Sihag的回答只有一个rfind

pos = a.rfind('.')
a[:pos] + '. -' + a[pos+1:]

Naïve approach:

a = "A long string with a . in the middle ending with ."
fchar = '.'
rchar = '. -'
a[::-1].replace(fchar, rchar[::-1], 1)[::-1]

Out[2]: 'A long string with a . in the middle ending with . -'

Aditya Sihag’s answer with a single rfind:

pos = a.rfind('.')
a[:pos] + '. -' + a[pos+1:]