I am wondering how it is possible to combine the following functions into one. The functions remove the entire word if "_" respectively "/" occur in a text.
I have tried the following, and the code fulfils it purpose. It his however cumbersome and I am wondering how to simplify it.
text = "This is _a default/ text"
def filter_string1(string):
a = []
for i in string.split():
if "_" not in i:
a.append(i)
return ' '.join(a)
def filter_string2(string):
a = []
for i in string.split():
if "/" not in i:
a.append(i)
return ' '.join(a)
text_no_underscore = filter_string1(text)
text_no_underscore_no_slash = filter_string2(text_no_underscore)
print(text_no_underscore_no_slash)
The output is (as desired):
"This is text"
You can combine the if conditions.
text = "This is _a default/ text"
def filter(string):
a = []
for i in string.split():
if "_" not in i and "/" not in i:
a.append(i)
return ' '.join(a)
print(filter(text))