-1

replace_ending 函数用新字符串替换句子中的旧字符串,但前提是句子以旧字符串结尾。如果句子中的旧字符串不止一次出现,则只替换末尾的一个,而不是全部替换。例如,replace_ending("abcabc", "abc", "xyz") 应该返回 abcxyz,而不是 xyzxyz 或 xyzabc。字符串比较区分大小写,因此 replace_ending("abcabc", "ABC", "xyz") 应返回 abcabc(未进行任何更改)。

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if ___:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = ___
    new_sentence = ___
    return new_sentence

# Return the original sentence if there is no match 
return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
4

29 回答 29

6

答案如下。这个问题属于谷歌学习python。

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
        i = sentence.rfind(old)
        new_sentence = sentence[:i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
于 2020-04-11T15:06:30.850 回答
4
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
        i = len(old)
        new_sentence = sentence[:-i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
于 2020-05-07T20:14:40.253 回答
2
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i=len(sentence) 
    l=len(old)
    new_sentence = sentence[0:i-l] + new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
于 2020-06-05T16:17:38.687 回答
1
def replace_ending(sentence, old, new):
     if sentence.endswith(old):
         i = sentence.rfind(old)
         new_sentence = sentence[:i]+new
         return new_sentence
     return sentence
 
于 2020-09-21T23:51:54.373 回答
1
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
   if sentence[-len(old):]==old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
      new_sentence = sentence[:-len(old)] + new
      return new_sentence

# Return the original sentence if there is no match 
   return sentence
于 2020-07-15T12:59:56.100 回答
1

以上答案对我没有帮助,我只是找出自己的答案。使用简单的 len() 函数。

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(old)
        new_sentence =sentence.replace("old","new")
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
Output:
It's raining cats and dogs
She sells seashells by the seashore
The weather is nice in May
The weather is nice in April

于 2020-08-13T17:04:39.560 回答
1
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(sentence)-len(old)
        new_sentence = sentence[:i]+ new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
于 2020-07-24T05:55:34.997 回答
1
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(old)
        new_sentence = sentence[:-i]+new
        return new_sentence
    # Return the original sentence if there is no match
    return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"

print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"

print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"

print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April
于 2020-05-17T10:05:18.350 回答
0

我实际上是在没有结尾字符串的情况下做到的

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    loc=len(old)
    x=sentence[-loc:]

    if x==old:
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence[:-loc]
        new_sentence = "{}{}".format(i,new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
于 2021-11-26T08:04:06.620 回答
0

我的解决方案没有 .endswith() 也没有 rindex():

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if old == sentence[-len(old):]:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = sentence[:-len(old)]
    new_sentence = i + new
    return new_sentence

# Return the original sentence if there is no match 
return sentence

问候!

于 2022-02-08T20:01:47.143 回答
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence
old_index=sentence.rindex(" ")+1 
if sentence[old_index:]==old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = old_index
    new_sentence = sentence[:i]+new
    return new_sentence

# Return the original sentence if there is no match 
return sentence
于 2021-11-11T18:12:51.090 回答
0
if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.split()
        i[-1] = new
        new_sentence = " ".join(i)
        return new_sentence
于 2021-11-25T19:06:59.203 回答
0

因为idk如果python中有一个库来检查最后一句(.endswith())所以我让我的代码从后面手动索引字符串,然后比较它是否相同。

def replace_ending(sentence, old, new):
    new_sentence = ""
    n = 0
    for letter in range(len(sentence)): '''this loop is used to find n index of the same word '''
        dump = sentence[(len(sentence)-(1+letter))] 
        new_sentence = dump + new_sentence '''manually create sentence from behind, 
                                           and check if it's has the same word'''
        if old in new_sentence: '''if it has, we save the number of index.'''
            n = len(sentence)-(1+letter)
            break
    splitted = sentence.split()
    i = len(splitted)
    if old in splitted[i-1]: '''check if the same word exactly the last word. If 
                               it's then replace with new word'''
        sentence = sentence[:n] + new
    return sentence
于 2022-02-20T08:15:08.013 回答
0

The solution is even simpler:

def replace_ending(sentence, old, new):
    if sentence.endswith(old):
        i = -len(old)
        sentence = sentence[:i] + new
    return sentence
于 2021-05-24T16:19:41.180 回答
0

看到这个问题,我首先想到的是replace()方法。但是,与split() 方法不同;没有lsplit()rsplit()选项。那么,我们如何创建一个rreplace().

我偶然发现了另一个线程:rreplace()

现在,这很容易解决。

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if sentence.endswith(old):
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    new_sentence = new.join(sentence.rsplit(old,1))
    return new_sentence

# Return the original sentence if there is no match 
return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
于 2021-08-26T22:55:07.613 回答
0

这是我的解决方案,我尝试了不同的方法。我利用了先创建列表的优势,然后通过“加入”方法“展开”:

def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
sentence_list = sentence.split()
if sentence_list[-1] == old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    sentence_list[-1] = new
    new_sentence = " ".join(sentence_list)
    return new_sentence

# Return the original sentence if there is no match 
return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
于 2021-09-24T17:19:58.963 回答
0
def replace_ending(sentence, old, new):

# Check if the old string is at the end of the sentence 

if sentence.endswith(old):

    # Using i as the slicing index, combine the part

    # of the sentence up to te matched string at the 

    # end with the new string

    i = sentence.rfind(old)

    new_sentence = sentence[:--i]+new

    return new_sentence

# Return the original sentence if there is no match 

return sentence
于 2022-01-26T06:16:22.963 回答
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = len(sentence) - len(old)
        new_sentence = sentence[:i] + new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

您还可以通过单击此处查看片段

于 2020-09-04T11:54:56.477 回答
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
sen=sentence.split()
if sen[-1] == old:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = len(old)
    new_sentence = sentence[:-i]+new

    return new_sentence

# Return the original sentence if there is no match 
return sentence
于 2020-06-18T03:10:00.370 回答
0
def replace_ending(sentence, old, new):
# Check if the old string is at the end of the sentence 
if old in sentence:
    # Using i as the slicing index, combine the part
    # of the sentence up to the matched string at the 
    # end with the new string
    i = sentence.rfind(old)
    last_txt = sentence[-(len(old)):]
    if last_txt == old:
        new_sentence = sentence[:i]+new
    else:
        return sentence
    return new_sentence
# Return the original sentence if there is no match 
return sentence
于 2021-12-23T10:03:07.947 回答
0
    def replace_ending(sentence, old, new):
        # Check if the old string is at the end of the sentence 
        if sentence.endswith(old):
            # Using i as the slicing index, combine the part
            # of the sentence up to the matched string at the 
            # end with the new string
            i = sentence.rfind(old) #rfind() finds the last occurrence of the substring
            new_sentence = sentence[:i]+new
            return new_sentence
    # Return the original sentence if there is no match 
    return sentence
于 2020-06-14T10:09:06.310 回答
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    endsentence= sentence.split()
    if endsentence[-1] == old:
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = endsentence[-len(endsentence):-1]
        concatenate_new_sentence = i + [new]
        new_sentence=" ".join(concatenate_new_sentence)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
于 2022-01-05T15:38:20.467 回答
0

我找到了另一个使用“.rindex”的解决方案。我认为它类似于使用“len()”。

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith (old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.rindex(old)
        new_sentence =sentence[:i]+str(new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

于 2021-06-17T17:28:07.697 回答
0
# Replace word endings python code( checking if the end of the sentence is same as "old" and replacing with "new")
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    sentence = sentence.split()
    old = old.split()
    new = new.split()
    new_sentence = " "
    for word in old:
        for words in new:
            if (sentence[-1] == word):
                new_sentence = new_sentence.join(sentence[0:-1] + new)
            else:
                new_sentence = new_sentence.join(sentence)
            return new_sentence
    return sentence
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
    
于 2022-02-20T10:55:13.053 回答
0
def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.rfind(old)
        new_sentence = sentence[:i] + str(new)
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
于 2021-05-23T18:36:05.337 回答
0

. i = sentence.split() 。# 这会将每个单词中的句子拆分为 list 的一个元素。z = i[:-1] + [新] 。# 这将通过 .using 切片函数再次添加列表的所有元素,除了最后一个元素,这就是为什么我从开始到直到 (n-1) .new_sentence = ' '.join(z) 使用切片的原因。# 这将再次加入列表的元素 –</p>

     def replace_ending(sentence, old, new):
            # Check if the old string is at the end of the sentence 
            if sentence.endswith(old):
                # Using i as the slicing index, combine the part
                # of the sentence up to the matched string at the 
                # end with the new string
                i = sentence.split()
                z = i[:-1] + [new]
        
                new_sentence = ' '.join(z)
                return new_sentence
        
            # Return the original sentence if there is no match 
            return sentence
            
        print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
        # Should display "It's raining cats and dogs"
        print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
        # Should display "She sells seashells by the seashore"
        print(replace_ending("The weather is nice in May", "may", "april")) 
        # Should display "The weather is nice in May"
        print(replace_ending("The weather is nice in May", "May", "April")) 
        # Should display "The weather is nice in April"
于 2021-08-29T11:53:29.453 回答
0

这对我有用。

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if old in sentence.rsplit(' ', 1):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i =  sentence.rsplit(' ', 1)[0] + str(' ') + new
        new_sentence = i
        return new_sentence

    # Return the original sentence if there is no match
    return sentence

print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", 
"donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"
于 2021-04-30T12:13:31.197 回答
0

上面的答案真的对我不起作用,所以我花了一些时间终于找到了一个可行的解决方案。它只是重新检查单词并获取它的索引。

def replace_ending(sentence, old, new):
    # Check if the old string is at the end of the sentence 
    if sentence.endswith(old):
        # Using i as the slicing index, combine the part
        # of the sentence up to the matched string at the 
        # end with the new string
        i = sentence.find(old)
        if(sentence[i+ len(old):].endswith(old)):
            j=sentence[i + len(old):].find(old)
            new_sentence=sentence[:i+ len(old)+j] + new
            return new_sentence
        new_sentence = sentence[:i] + new
        return new_sentence

    # Return the original sentence if there is no match 
    return sentence
    
print(replace_ending("It's raining cats and cats", "cats", "dogs")) 
# Should display "It's raining cats and dogs"
print(replace_ending("She sells seashells by the seashore", "seashells", "donuts")) 
# Should display "She sells seashells by the seashore"
print(replace_ending("The weather is nice in May", "may", "april")) 
# Should display "The weather is nice in May"
print(replace_ending("The weather is nice in May", "May", "April")) 
# Should display "The weather is nice in April"

于 2020-06-20T13:06:34.960 回答
-1
def replace_ending(sentence,old,new):
    divide= list(sentence.split(" "))
    if divide[-1] == old:
        return ' '.join(divide[:-1])+" "+new
    else:
        return sentence
于 2021-08-03T05:38:00.190 回答