How to replace repeated instances of a character with a single instance of that character in python

前端 未结 11 1294
北海茫月
北海茫月 2020-12-31 00:29

I want to replace repeated instances of the \"*\" character within a string with a single instance of \"*\". For example if the string is \"*

相关标签:
11条回答
  • 2020-12-31 00:44
    re.sub('\*+', '*', pattern)
    

    That will do.

    0 讨论(0)
  • 2020-12-31 00:45

    This will work for any number of consecutive asterisks, although you may need to replace the tilde with some other string that you know will be unique throughout the string.

        string = "begin*************end"
    
        string.replace("**", "~*").replace("*~", "").replace("~*", "*").replace("**", "*")
    

    I believe regex approaches would be generally more computationally expensive than this.

    0 讨论(0)
  • 2020-12-31 00:47

    The naive way to do this kind of thing with re is

    re.sub('\*+', '*', text)
    

    That replaces runs of 1 or more asterisks with one asterisk. For runs of exactly one asterisk, that is running very hard just to stay still. Much better is to replace runs of TWO or more asterisks by a single asterisk:

    re.sub('\*\*+', '*', text)
    

    This can be well worth doing:

    \python27\python -mtimeit -s"t='a*'*100;import re" "re.sub('\*+', '*', t)"
    10000 loops, best of 3: 73.2 usec per loop
    
    \python27\python -mtimeit -s"t='a*'*100;import re" "re.sub('\*\*+', '*', t)"
    100000 loops, best of 3: 8.9 usec per loop
    

    Note that re.sub will return a reference to the input string if it has found no matches, saving more wear and tear on your computer, instead of a whole new string.

    0 讨论(0)
  • 2020-12-31 00:52

    how about a non regex way

    def squeeze(char,s):
        while char*2 in s:
            s=s.replace(char*2,char)
        return s
    print(squeeze("*" , "AB***abc**def**AA***k"))
    

    This returns AB*abc*def*AA*k

    0 讨论(0)
  • 2020-12-31 00:54

    without regexp you can use general repeating element removal with checking of '*':

    source = "***abc**dee*fg******h"
    target = ''.join(c for c,n in zip(source, source[1:]+' ') if  c+n != '**')
    print target
    
    0 讨论(0)
提交回复
热议问题