2

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"
1

2 Answers 2

3

You can use replace:

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

string = "#tag #bold bob #merge"

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

print(string)
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.