Monday, 15 February 2010

Python Snippets

With a regular expression,

import re
newstring = re.sub(r"\b(\w)(?![2'])", r'\1x', oldstring)

should be fine. If you're allergic to res,

news = ' '.join(x + 'x' if len(x)==1 else x for x in olds.split())

is a concise way of expressing a similar transformation (if length-one is really the only thing you need to check before appending 'x' to an item).

No comments: