Fastest implementation to do multiple string substitutions in Python

后端 未结 3 523
攒了一身酷
攒了一身酷 2020-12-14 23:30

Is there any recommended way to do multiple string substitutions other than doing replace chaining on a string (i.e. text.replace(a, b).replace(c, d).repl

3条回答
  •  眼角桃花
    2020-12-15 00:24

    Something like the following maybe? Split the text into pieces with the first "from" item to be replaced, then recursively split each of those parts into sub-parts with the next "from" item to be replaced, and so on, until you've visited all your replacements. Then join with the "to" replacement item for each as recursive function completes.

    A little hard to wrap your head around the following code perhaps (it was for me, and I wrote it), but it seems to function as intended. I didn't benchmark it, but I suspect it would be reasonably fast.

    def multi_replace(pairs, text):
        stack = list(pairs)
        stack.reverse()
        def replace(stack, parts):
            if not stack:
                return parts
            # copy the stack so I don't disturb parallel recursions
            stack = list(stack) 
            from_, to = stack.pop()
            #print 'split (%r=>%r)' % (from_, to), parts
            split_parts = [replace(stack, part.split(from_)) for part in parts]
            parts = [to.join(split_subparts) for split_subparts in split_parts]
            #print 'join (%r=>%r)' % (from_, to), parts
            return parts
        return replace(stack, [text])[0]
    
    
    print multi_replace(
        [('foo', 'bar'), ('baaz', 'foo'), ('quux', 'moop')], 
        'foobarbaazfooquuxquux')
    

    for:

    barbarfoobarmoopmoop
    

提交回复
热议问题