I have a bunch of replace functions to modify a list of strings that I have . Originally, I had written out each list comprehension of what strings I want to be replaced.
ls = ['sentence with string a', 'sentence with string d and string a',
'sentence with string d', 'sentence with string b and string b again',
'sentence with string c', 'sentence with string c and string d']
ls = [x.replace('string a' , '') for x in ls]
ls = [x.replace('string b' , '') for x in ls]
ls = [x.replace('string c' , '') for x in ls]
ls = [x.replace('string d' , '') for x in ls]
ls = ['sentence with', 'sentence with and',
'sentence with', 'sentence with and again',
'sentence with', 'sentence with and']
However, since the strings I want to replace may change I want to to use a list to do all the replace functions. When I try a for loop, this ends up removing characters from the middle of the string and doesn't give me the list like before.
words = ['string a', 'string b', 'string c', 'string d']
for txt in words:
ls = [x.replace(txt, "") for x in ls]
How can I use the list of strings to achieve the same result?
CodePudding user response:
You can use a comprehension and functools.reduce
:
from functools import reduce
words = ['string a', 'string b', 'string c', 'string d']
ls = ['sentence with string a', 'sentence with string d and string a',
'sentence with string d', 'sentence with string b and string b again',
'sentence with string c', 'sentence with string c and string d']
[reduce(lambda s, w: s.replace(w, ""), words, sent) for sent in ls]
# ['sentence with ', 'sentence with and ', 'sentence with ',
# 'sentence with and again', 'sentence with ', 'sentence with and ']
CodePudding user response:
Using re
:
reg = re.compile('string [abcd]')
Then you can just use that compiled regex in the comprehension:
[reg.sub('', s) for s in ls]
Result:
['sentence with ',
'sentence with and ',
'sentence with ',
'sentence with and again',
'sentence with ',
'sentence with and ']
CodePudding user response:
Seems that Jab was faster than me, but in case you want to keep your list of words more flexible, here's a similar solution:
import re
pattern = '|'.join(words)
[re.sub(pattern , '', x).strip() for x in ls]
results in
['sentence with',
'sentence with and',
'sentence with',
'sentence with and again',
'sentence with',
'sentence with and']