Home > OS >  How to remove all first articles from a string?
How to remove all first articles from a string?

Time:11-02

I have a list where i want to remove all first articles: ["the house", "the beautiful garten", "the beautiful garten of the house"]

and i want the list to only contain: ["house", "beautiful garten", "beautiful garten of the house"]

If the first word is an article, then remove. In case the articles appears in the sentence, they should be kept.

CodePudding user response:

If you're working with python try

new_list = [s[4:] if s.startswith('the ') else s for s in old_list]

CodePudding user response:

Another option is using re (this example will also ignore whitespaces around the first the:

import re

lst = ["the house", "the beautiful garten", "the beautiful garten of the house"]

pat = re.compile(r"^\s*the\s ", flags=re.I)
out = [pat.sub("", w) for w in lst]

print(out)

Prints:

['house', 'beautiful garten', 'beautiful garten of the house']

CodePudding user response:

If you have a list of potential articles or words to be trimmed, you can do the following to remove that word plus the following space:

articles = ["the", "a", "an"]

sentences = ["the house", "a beautiful garten", "an amazing garten of the house"]


out = []
for s in sentences:
    new = s
    for a in articles:
      if s.startswith(f'{a} '):
        new = s[len(a) 1:]
    out.append(new)

print(out)

['house', 'beautiful garten', 'amazing garten of the house']
  • Related