mercredi 11 mars 2015

Replacing words in text using dictionary


Vote count:

0




I am trying to create a program that replaces word from a string.



ColorPairs = {'red':'blue','blue':'red'}

def ColorSwap(text):
for key in ColorPairs:
text = text.replace(key, ColorPairs[key])
print text

ColorSwap('The blue ball')
# 'The blue ball'


This program works when I put in 'The red ball' instead of 'The blue ball'. I am stuck trying to figure out a way to make it so that the program doesn't override the first replaced key. Please help.





asked 1 min ago







Replacing words in text using dictionary

Aucun commentaire:

Enregistrer un commentaire