Mass string replace in python?

前端 未结 13 2236
我寻月下人不归
我寻月下人不归 2020-11-28 20:14

Say I have a string that looks like this:

str = \"The &yquick &cbrown &bfox &Yjumps over the &ulazy dog\"

You\'ll notic

13条回答
  •  心在旅途
    2020-11-28 21:05

    Try this, making use of regular expression substitution, and standard string formatting:

    # using your stated values for str and dict:
    >>> import re
    >>> str = re.sub(r'(&[a-zA-Z])', r'%(\1)s', str)
    >>> str % dict
    'The \x1b[0;30mquick \x1b[0;31mbrown \x1b[0;32mfox \x1b[0;33mjumps over the \x1b[0;34mlazy dog'
    

    The re.sub() call replaces all sequences of ampersand followed by single letter with the pattern %(..)s containing the same pattern.

    The % formatting takes advantage of a feature of string formatting that can take a dictionary to specify the substitution, rather than the more commonly occurring positional arguments.

    An alternative can do this directly in the re.sub, using a callback:

    >>> import re
    >>> def dictsub(m):
    >>>    return dict[m.group()]
    >>> str = re.sub(r'(&[a-zA-Z])', dictsub, str)
    

    This time I'm using a closure to reference the dictionary from inside the callback function. This approach could give you a little more flexibility. For example, you could use something like dict.get(m.group(), '??') to avoid raising exceptions if you had strings with unrecognized code sequences.

    (By the way, both "dict" and "str" are builtin functions, and you'll get into trouble if you use those names in your own code much. Just in case you didn't know that. They're fine for a question like this of course.)

    Edit: I decided to check Tor's test code, and concluded that it's nowhere near representative, and in fact buggy. The string generated doesn't even have ampersands in it (!). The revised code below generates a representative dictionary and string, similar to the OP's example inputs.

    I also wanted to verify that each algorithm's output was the same. Below is a revised test program, with only Tor's, mine, and Claudiu's code -- because the others were breaking on the sample input. (I think they're all brittle unless the dictionary maps basically all possible ampersand sequences, which Tor's test code was doing.) This one properly seeds the random number generator so each run is the same. Finally, I added a minor variation using a generator which avoids some function call overhead, for a minor performance improvement.

    from time import time
    import string
    import random
    import re
    
    random.seed(1919096)  # ensure consistent runs
    
    # build dictionary with 40 mappings, representative of original question
    mydict = dict(('&' + random.choice(string.letters), '\x1b[0;%sm' % (30+i)) for i in range(40))
    # build simulated input, with mix of text, spaces, ampersands in reasonable proportions
    letters = string.letters + ' ' * 12 + '&' * 6
    mystr = ''.join(random.choice(letters) for i in range(1000))
    
    # How many times to run each solution
    rep = 10000
    
    print('Running %d times with string length %d and %d ampersands'
        % (rep, len(mystr), mystr.count('&')))
    
    # Tor Valamo
    # fixed from Tor's test, so it actually builds up the final string properly
    t = time()
    for x in range(rep):
        output = mystr
        for k, v in mydict.items():
            output = output.replace(k, v)
    print('%-30s' % 'Tor fixed & variable dict', time() - t)
    # capture "known good" output as expected, to verify others
    expected = output
    
    # Peter Hansen
    
    # build charset to use in regex for safe dict lookup
    charset = ''.join(x[1] for x in mydict.keys())
    # grab reference to method on regex, for speed
    patsub = re.compile(r'(&[%s])' % charset).sub
    
    t = time()
    for x in range(rep):
        output = patsub(r'%(\1)s', mystr) % mydict
    print('%-30s' % 'Peter fixed & variable dict', time()-t)
    assert output == expected
    
    # Peter 2
    def dictsub(m):
        return mydict[m.group()]
    
    t = time()
    for x in range(rep):
        output = patsub(dictsub, mystr)
    print('%-30s' % 'Peter fixed dict', time() - t)
    assert output == expected
    
    # Peter 3 - freaky generator version, to avoid function call overhead
    def dictsub(d):
        m = yield None
        while 1:
            m = yield d[m.group()]
    
    dictsub = dictsub(mydict).send
    dictsub(None)   # "prime" it
    t = time()
    for x in range(rep):
        output = patsub(dictsub, mystr)
    print('%-30s' % 'Peter generator', time() - t)
    assert output == expected
    
    # Claudiu - Precompiled
    regex_sub = re.compile("(%s)" % "|".join(mydict.keys())).sub
    
    t = time()
    for x in range(rep):
        output = regex_sub(lambda mo: mydict[mo.string[mo.start():mo.end()]], mystr)
    print('%-30s' % 'Claudio fixed dict', time() - t)
    assert output == expected
    

    I forgot to include benchmark results before:

        Running 10000 times with string length 1000 and 96 ampersands
        ('Tor fixed & variable dict     ', 2.9890000820159912)
        ('Peter fixed & variable dict   ', 2.6659998893737793)
        ('Peter fixed dict              ', 1.0920000076293945)
        ('Peter generator               ', 1.0460000038146973)
        ('Claudio fixed dict            ', 1.562000036239624)
    

    Also, snippets of the inputs and correct output:

    mystr = 'lTEQDMAPvksk k&z Txp vrnhQ GHaO&GNFY&&a...'
    mydict = {'&p': '\x1b[0;37m', '&q': '\x1b[0;66m', '&v': ...}
    output = 'lTEQDMAPvksk k←[0;57m Txp vrnhQ GHaO←[0;67mNFY&&a P...'
    

    Comparing with what I saw from Tor's test code output:

    mystr = 'VVVVVVVPPPPPPPPPPPPPPPXXXXXXXXYYYFFFFFFFFFFFFEEEEEEEEEEE...'
    mydict = {'&p': '112', '&q': '113', '&r': '114', '&s': '115', ...}
    output = # same as mystr since there were no ampersands inside
    

提交回复
热议问题