[Python-ideas] in str.replace(old, new), allow 'old' to accept a tuple
INADA Naoki
songofacandy at gmail.com
Fri Apr 13 03:00:33 CEST 2012
> Simpler, maybe, at least at the API level. But faster? Not necesarily. It
> could use Aho-Corasick, but that means it needs to construct the search
> graph on each call, which is fairly expensive.
You're right. But in simple situation, overhead of making match object and
calling callback is more expensive. (ex. https://gist.github.com/2369648 )
I think chaining replace is not so bad for such simple cases.
So a problem is there are no "one obvious way" to replace multiple keywords.
On Fri, Apr 13, 2012 at 2:08 AM, Stefan Behnel <stefan_ml at behnel.de> wrote:
> INADA Naoki, 12.04.2012 18:32:
>> On Fri, Apr 13, 2012 at 1:20 AM, Sven Marnach wrote:
>>> INADA Naoki schrieb am Thu, 12. Apr 2012, um 22:17:30 +0900:
>>>> Oh, I didn't know that. Thank you.
>>>> But what about unescape? str.translate accepts only one character key.
>>>
>>> You'd currently need to use the `re` module:
>>>
>>> >>> d = {"&": "&", ">": ">", "<": "<"}
>>> >>> re.sub("|".join(d), lambda m: d[m.group()], "<>&")
>>> '<>&'
>>
>> Yes, I know it.
>> But if str.replace() or str.translate() can do it, it is simpler and
>> faster than re.sub().
>
> Simpler, maybe, at least at the API level. But faster? Not necesarily. It
> could use Aho-Corasick, but that means it needs to construct the search
> graph on each call, which is fairly expensive. And str.replace() isn't the
> right interface for anything but a one-shot operation if the intention is
> to pass in a sequence of keywords.
>
> Stefan
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
--
INADA Naoki <songofacandy at gmail.com>
More information about the Python-ideas
mailing list