Search code examples
pythonfunctional-programmingpython-itertoolsfunctools

itertools or functools for find-replace list in python


I have a set of strings that are sometimes invalid that I'd like to replace with particular better strings. I've been playing with functools and itertools and would like to try applying these to the problem, but I'm a little stuck. Here's what I have:

s1 = 'how to sing songs'
s2 = 'This is junk'
s3 = "This is gold"
s4 = 'HTML'
s5 = 'html'
s6 = 'i'
mylist = [s1,s2,s3,s4,s5,s6]

replacements = [('html','HTML'),('how to sing songs','singing'),('This is junk', ''),('i','')]

I'd like a function algorithm that sort of says, for each string in mylist, for each replacement in replacements, string.replace(replacement[0],replacement[1]).

What kind of came to mind was something like...

map(lambda x,y: x.replace(y[0],y[1]),mylist,replacements)
map(partial(lambda x,y: x.replace(y[0],y[1]),mylist),replacements)

But the first wanted extra arguments, and the second said list object has no attribute replace. Is there a slick functional way to solve this?


Solution

  • >>> [reduce(lambda x, y: x.replace(y[0], y[1]), replacements, s) for s in mylist]
    ['singing', '', 'This is gold', 'HTML', 'HTML']
    

    Equivalent code with map() instead of a list comprehension:

    map(partial(reduce, lambda x, y: x.replace(y[0], y[1]), replacements), mylist)