joshual
joshual

Reputation: 43

Remove substring from string if substring is contained in a list

Problem: I have a string that has "tags" from a list called "listOfTags". If the string contains one of these "tags" from the list I want to be able to remove these "tags" from the string.

What I tried: I first tried traversing through "listOfTags" and appending each "tag" to an empty string variable called x. Then, I tried to remove the "tags" from another string variable called y by using the string.replace method.I then realized that this method would only return what I wanted if the "tags" appeared in the order that they were appended in the variable x.

The algorithm I created is as follows:

if a string contains as a sub-string any strings specified in a particular list: remove the substring from the string

An example of the problem:

listOFTags = ["#tag", "#bold", "#merge"] 

string = "#tag #bold bob #merge" 

#execute algorithm here

How do I get a string returned that has the text "bob"?

What I want returned:

new_string = "bob"

Upvotes: 2

Views: 1888

Answers (2)

GZ0
GZ0

Reputation: 4268

If there are tens of tags and each string to process contains tens of words, it would be more efficient to use regular expressions for this task.

import re
p = re.compile('|'.join(listOFTags))  # p can be reused across different input strings
new_string = p.sub("", string).strip()

This solution works if the tags do not contain any characters that have special meanings in regular expressions.

Upvotes: 1

Gravity Mass
Gravity Mass

Reputation: 605

You can use replace:

listOFTags = ["#tag", "#bold", "#merge"]

string = "#tag #bold bob #merge"

for tags in listOFTags:
    string = string.replace(tags, "")

print(string)

Upvotes: 3

Related Questions