Find and Replace Simplification

Joshua Landau joshua at landau.ws
Sun Jul 21 14:49:29 CEST 2013


On 21 July 2013 13:28, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 21.07.13 14:29, Joshua Landau написав(ла):
>
>> On 21 July 2013 08:44, Serhiy Storchaka <storchaka at gmail.com> wrote:
>>>
>>> 20.07.13 20:03, Joshua Landau написав(ла):
>>>
>>>> Still, it seems to me that it should be optimizable for sensible
>>>> builtin types such that .translate is significantly faster, as there's
>>>> no theoretical extra work that .translate *has* to do that .replace
>>>> does not, and .replace also has to rebuild the string a lot of times.
>>>
>>>
>>> You should analyze overall mapping and reorder items in right order (if
>>> it
>>> possible), i.e. '&' should be replaced before '<' in html.escape. This
>>> extra
>>> work is too large for most real input.
>>
>>
>> I don't understand. What items are you reordering?
>
>
> mapping.items(). We can implement s.translate({ord('<'): '<', ord('&'):
> '&'}) as s.replace('&', '&').replace('<', '<'), but not as
> s.replace('<', '<').replace('&', '&').

I see -- that won't always be the case though, as there can be "loops"
aka "ab" -> "ba".



More information about the Python-list mailing list