Python:用另一个词替换第一个出现的词
Python: Replace First Word That Shows Up with Another Word
我有七种水果,你可以在一个句子中使用,你可以用不止一种,想写多少次都可以。它们是 Watermelon
、Pear
、Peach
、Orange
、Apple
、Banana
和 Grapes
。我想报告这句话并替换出现的第一个水果,无论该词是否多次显示并且允许列表中给出的多个水果都带有球芽甘蓝,如下所示。
In: Apple apple apple what is a watermelon have to do to get an apple?
Out: Brussel sprouts apple apple what is a watermelon have to do to get an
apple?
In: The apple likes orange
Out: The brussel sprouts likes orange
现在我正在弄乱下面的代码,但这只适用于一种水果,我需要同时检查所有七个,看看哪个是第一个,然后将其替换。
print sentence.replace("apple", "brussel sprouts", 1)
我该怎么做?
这里有两个不同的问题;第一个是 find
'ing 水果的位置(如果它存在于字符串中)。第二个是第一个被发现的 replace
'ing。由于我不想为您解决家庭作业问题,因此我将简单地向您展示一个小示例,以帮助您朝着正确的方向开始。
sentence = "Apple apple apple what is a watermelon have to do to get an apple?".lower() # We lowercase it so that "Apple" == "apple", etc.
index = sentence.find("apple")
print(index)
>>> 0
index = sentence.find("banana") # This is what happens when searching for fruit not in sentence
print(index)
>>> -1
既然您了解了 find
,您应该能够轻松地弄清楚如何组合一系列 find
和 replace
操作来获得所需的输出。
通过re.sub
。 (.*?)
at the first 有助于捕获第一个水果之前的所有字符。模式 (?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)
匹配第一个水果名称。因此,通过将匹配的字符替换为组索引 1 内的字符,将为您提供所需的输出。
>>> import re
>>> s = "Apple apple apple what is a watermelon have to do to get an apple?"
>>> re.sub(r'(?i)^(.*?)\b(?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)\b', r'brussel sprouts', s)
'brussel sprouts apple apple what is a watermelon have to do to get an apple?'
>>> re.sub(r'(?i)^(.*?)\b(?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)\b', r'brussel sprouts', 'The apple likes orange')
'The brussel sprouts likes orange'
(?i)
调用了不区分大小写的修饰符,它强制正则表达式引擎进行不区分大小写的匹配。
我有七种水果,你可以在一个句子中使用,你可以用不止一种,想写多少次都可以。它们是 Watermelon
、Pear
、Peach
、Orange
、Apple
、Banana
和 Grapes
。我想报告这句话并替换出现的第一个水果,无论该词是否多次显示并且允许列表中给出的多个水果都带有球芽甘蓝,如下所示。
In: Apple apple apple what is a watermelon have to do to get an apple?
Out: Brussel sprouts apple apple what is a watermelon have to do to get an apple?
In: The apple likes orange
Out: The brussel sprouts likes orange
现在我正在弄乱下面的代码,但这只适用于一种水果,我需要同时检查所有七个,看看哪个是第一个,然后将其替换。
print sentence.replace("apple", "brussel sprouts", 1)
我该怎么做?
这里有两个不同的问题;第一个是 find
'ing 水果的位置(如果它存在于字符串中)。第二个是第一个被发现的 replace
'ing。由于我不想为您解决家庭作业问题,因此我将简单地向您展示一个小示例,以帮助您朝着正确的方向开始。
sentence = "Apple apple apple what is a watermelon have to do to get an apple?".lower() # We lowercase it so that "Apple" == "apple", etc.
index = sentence.find("apple")
print(index)
>>> 0
index = sentence.find("banana") # This is what happens when searching for fruit not in sentence
print(index)
>>> -1
既然您了解了 find
,您应该能够轻松地弄清楚如何组合一系列 find
和 replace
操作来获得所需的输出。
通过re.sub
。 (.*?)
at the first 有助于捕获第一个水果之前的所有字符。模式 (?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)
匹配第一个水果名称。因此,通过将匹配的字符替换为组索引 1 内的字符,将为您提供所需的输出。
>>> import re
>>> s = "Apple apple apple what is a watermelon have to do to get an apple?"
>>> re.sub(r'(?i)^(.*?)\b(?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)\b', r'brussel sprouts', s)
'brussel sprouts apple apple what is a watermelon have to do to get an apple?'
>>> re.sub(r'(?i)^(.*?)\b(?:Watermelon|Pear|Peach|Orange|Apple|Banana|Grapes)\b', r'brussel sprouts', 'The apple likes orange')
'The brussel sprouts likes orange'
(?i)
调用了不区分大小写的修饰符,它强制正则表达式引擎进行不区分大小写的匹配。