mutliple search and replace

Emile van Sebille emile at fenx.com
Wed Mar 27 21:13:48 EST 2002


Trung Hoang
> Suppose i have a map (or two lists). is it possible to make a sweep
through
> a string to find all occurances of key's in the map then replace them
all in
> one sweep?
>

>>> d = {'one':'een','two':'twee','three':'drie','four':'vier'}
>>> s = "one two buckle my shoe three four je t'adore"
>>> ' '.join([d.get(w,w) for w in s.split()])
"een twee buckle my shoe drie vier je t'adore"


--

Emile van Sebille
emile at fenx.com

---------




More information about the Python-list mailing list