Tuesday, 15 May 2012

python - Replace substrings with elements of a list of tuples -



python - Replace substrings with elements of a list of tuples -

i have string such:

string = """ foobar_tent\n missyamica_ole """

this list of tuples have:

t = [("foo","virtue"), ("ole, "allo")]

i'm trying replace every instance of first element of each tuple sec element of each tuple. result this:

newstring = """ virtuebar_tent missyamica_allo """"

i tried doing such, doesn't alter anything.

newstring = "" a,b in t: newstring = string.replace(a,b)

you right. main issue doing string.replace each time lastly of replacements in list have worked.

newstring = string a,b in t: newstring = newstring.replace(a,b)

python replace tuples

No comments:

Post a Comment