Replace accented chars with unaccented ones
Jeff Epler
jepler at unpythonic.net
Tue Mar 16 08:57:08 EST 2004
On Mon, Mar 15, 2004 at 06:19:00PM -0800, Josiah Carlson wrote:
> Translating the replacements pairs into a dictionary would result in a
> significant speedup for large numbers of replacements.
>
> mapping = dict(replacement_pairs)
>
> def multi_replace(inp, mapping=mapping):
> return u''.join([mapping.get(i, i) for i in inp])
>
> One pass through the file gives an O(len(inp)) algorithm, much better
> (running-time wise) than the string.replace method that runs in
> O(len(inp) * len(replacement_pairs)) time as given.
Thanks for posting this. My other code was pretty hopeless, but for
some reason .get(i, i) didn't come to mind as a solution.
Jeff
More information about the Python-list
mailing list