热门标签:
Q:

仅当出现在字符串开头时才替换短语[duplicate]

例如,必须按此顺序删除"the ", "and ", "a ", "an ", "this " 或者"that ",只有当它们在字符串的开头时:

输入--->"the computer is the machine in charge of data processing processes"

输出--->"computer is the machine in charge of data processing processes"

重要的是,如果我设法发现句子以其中一个单词开头,我将其删除,然后不要继续尝试删除其他单词。 在这个例子中,它会检测字符串开头的单词"the ",删除它,不再尝试其余的单词。

要得出结论,你不应该删除任何东西,你必须是,或者如果你已经尝试删除所有6个选项("the ", "and ", "a ", "an ", "this ""that "),并且如果您没有发现输入短语以任何这些选项开头,那么假设您不应该删除任何内容。

我已经尝试过这样的事情,但问题是它会做所有的检查,而不仅仅是试图找到,直到比赛。

input_phrase.replace("the ","")

input_phrase = "An airplane is an aircraft with a higher density than the air."
input_phrase = input_phrase.lower()

input_phrase = input_phrase.replace("the ","",1)
input_phrase = input_phrase.replace("and ","",1)
input_phrase = input_phrase.replace("a ","",1)
input_phrase = input_phrase.replace("an ","",1)
input_phrase = input_phrase.replace("this ","",1)
input_phrase = input_phrase.replace("that ","",1)

output_phrase = input_phrase

print(repr(output_phrase))

该代码的问题在于,它不只是删除开头的单词,而是删除第一个匹配项,并且还使用all.remove(),而不是在已经删除其中一个匹配项时停止。

原网址
A:

以下是使用正则表达式执行此操作的一种方法:

import re

input_phrase = "An airplane is an aircraft with a higher density than the air."
output_phrase = re.sub(r"^(the|and|a|an|this|that) ", '', input_phrase, flags=re.IGNORECASE)
print(output_phrase)
  • re.ignorecase标志允许Anan工作。
  • ^用于断言字符串开头的位置。

没有正则表达式,你可以使用startswith()并循环关键字。

input_phrase = "An airplane is an aircraft with a higher density than the air."
keywords = ["the ", "and ", "a ", "an ", "this ", "that "]

output_phrase = input_phrase
for word in keywords:
    if input_phrase.lower().startswith(word):
        output_phrase = input_phrase[len(word):]
        break
print(output_phrase)
  • break用于退出for循环,以免浪费时间检查其他单词。

所有回答

共 1 条

author avatar
input_phrase = "An airplane is an aircraft with a higher density than the air.".lower()

output_phrase = ''

words = ["the", "and ", "a ", "an ", "this", "that"]

if list(filter(input_phrase.startswith, words)) != []:
    input_phrase = input_phrase.split()
    input_phrase = input_phrase[1:]

for word in input_phrase:
    output_phrase += ' ' + word

print(output_phrase)     

相似问题