【问题标题】:Best way to replace multiple characters in a string?替换字符串中多个字符的最佳方法?
【发布时间】:2011-03-25 14:47:20
【问题描述】:

我需要替换一些字符如下:&\&, #\#, ...

我编码如下,但我想应该有更好的方法。有什么提示吗?

strs = strs.replace('&', '\&')
strs = strs.replace('#', '\#')
...

【问题讨论】:

标签: python string replace


【解决方案1】:

你总是要在前面加上反斜杠吗?如果是这样,请尝试

import re
rx = re.compile('([&#])')
#                  ^^ fill in the characters here.
strs = rx.sub('\\\\\\1', strs)

这可能不是最有效的方法,但我认为它是最简单的。

【讨论】:

    【解决方案2】:
    >>> string="abc&def#ghi"
    >>> for ch in ['&','#']:
    ...   if ch in string:
    ...      string=string.replace(ch,"\\"+ch)
    ...
    >>> print string
    abc\&def\#ghi
    

    【讨论】:

    • 为什么需要双反斜杠?为什么只是“\”不起作用?
    • 双反斜杠转义反斜杠,否则python会将“\”解释为仍在打开的字符串中的文字引号字符。
    • 为什么需要string=string.replace(ch,"\\"+ch)string.replace(ch,"\\"+ch) 还不够吗?
    • @MattSom replace() 不会修改原始字符串,而是返回一个副本。因此,您需要分配代码才能产生任何效果。
    • 你真的需要 if 吗?无论如何,这看起来像是替换将要执行的操作的重复。
    【解决方案3】:
    >>> a = '&#'
    >>> print a.replace('&', r'\&')
    \&#
    >>> print a.replace('#', r'\#')
    &\#
    >>> 
    

    您想使用“原始”字符串(由替换字符串前缀的“r”表示),因为原始字符串不会特别处理反斜杠。

    【讨论】:

      【解决方案4】:

      你可以考虑写一个通用的转义函数:

      def mk_esc(esc_chars):
          return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
      
      >>> esc = mk_esc('&#')
      >>> print esc('Learn & be #1')
      Learn \& be \#1
      

      通过这种方式,您可以使用应该转义的字符列表来配置您的函数。

      【讨论】:

        【解决方案5】:

        像这样简单地链接replace函数

        strs = "abc&def#ghi"
        print strs.replace('&', '\&').replace('#', '\#')
        # abc\&def\#ghi
        

        如果替换的数量更多,您可以通过这种通用方式进行操作

        strs, replacements = "abc&def#ghi", {"&": "\&", "#": "\#"}
        print "".join([replacements.get(c, c) for c in strs])
        # abc\&def\#ghi
        

        【讨论】:

          【解决方案6】:

          替换两个字符

          我对当前答案中的所有方法以及一个额外的方法进行了计时。

          输入字符串abc&def#ghi 并替换& -> \& 和# -> \#,最快的方法是将替换链接在一起,如下所示:text.replace('&', '\&').replace('#', '\#')

          每个功能的时间安排:

          • a) 1000000 次循环,3 次中的最佳:每个循环 1.47 μs
          • b) 1000000 次循环,3 次中的最佳:每个循环 1.51 微秒
          • c) 100000 次循环,3 次中的最佳:每个循环 12.3 μs
          • d) 100000 次循环,3 次中的最佳:每个循环 12 微秒
          • e) 100000 次循环,3 次中的最佳:每个循环 3.27 微秒
          • f) 1000000 次循环,3 次中的最佳:每个循环 0.817 μs
          • g) 100000 次循环,3 次中的最佳:每个循环 3.64 μs
          • h) 1000000 次循环,最佳 3 次:每个循环 0.927 μs
          • i) 1000000 次循环,3 次中的最佳:每个循环 0.814 μs

          以下是函数:

          def a(text):
              chars = "&#"
              for c in chars:
                  text = text.replace(c, "\\" + c)
          
          
          def b(text):
              for ch in ['&','#']:
                  if ch in text:
                      text = text.replace(ch,"\\"+ch)
          
          
          import re
          def c(text):
              rx = re.compile('([&#])')
              text = rx.sub(r'\\\1', text)
          
          
          RX = re.compile('([&#])')
          def d(text):
              text = RX.sub(r'\\\1', text)
          
          
          def mk_esc(esc_chars):
              return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
          esc = mk_esc('&#')
          def e(text):
              esc(text)
          
          
          def f(text):
              text = text.replace('&', '\&').replace('#', '\#')
          
          
          def g(text):
              replacements = {"&": "\&", "#": "\#"}
              text = "".join([replacements.get(c, c) for c in text])
          
          
          def h(text):
              text = text.replace('&', r'\&')
              text = text.replace('#', r'\#')
          
          
          def i(text):
              text = text.replace('&', r'\&').replace('#', r'\#')
          

          时间是这样的:

          python -mtimeit -s"import time_functions" "time_functions.a('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.b('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.c('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.d('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.e('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.f('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.g('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.h('abc&def#ghi')"
          python -mtimeit -s"import time_functions" "time_functions.i('abc&def#ghi')"
          

          替换 17 个字符

          这里有类似的代码可以做同样的事情,但要转义更多字符 (\`*_{}>#+-.!$):

          def a(text):
              chars = "\\`*_{}[]()>#+-.!$"
              for c in chars:
                  text = text.replace(c, "\\" + c)
          
          
          def b(text):
              for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','$','\'']:
                  if ch in text:
                      text = text.replace(ch,"\\"+ch)
          
          
          import re
          def c(text):
              rx = re.compile('([&#])')
              text = rx.sub(r'\\\1', text)
          
          
          RX = re.compile('([\\`*_{}[]()>#+-.!$])')
          def d(text):
              text = RX.sub(r'\\\1', text)
          
          
          def mk_esc(esc_chars):
              return lambda s: ''.join(['\\' + c if c in esc_chars else c for c in s])
          esc = mk_esc('\\`*_{}[]()>#+-.!$')
          def e(text):
              esc(text)
          
          
          def f(text):
              text = text.replace('\\', '\\\\').replace('`', '\`').replace('*', '\*').replace('_', '\_').replace('{', '\{').replace('}', '\}').replace('[', '\[').replace(']', '\]').replace('(', '\(').replace(')', '\)').replace('>', '\>').replace('#', '\#').replace('+', '\+').replace('-', '\-').replace('.', '\.').replace('!', '\!').replace('$', '\$')
          
          
          def g(text):
              replacements = {
                  "\\": "\\\\",
                  "`": "\`",
                  "*": "\*",
                  "_": "\_",
                  "{": "\{",
                  "}": "\}",
                  "[": "\[",
                  "]": "\]",
                  "(": "\(",
                  ")": "\)",
                  ">": "\>",
                  "#": "\#",
                  "+": "\+",
                  "-": "\-",
                  ".": "\.",
                  "!": "\!",
                  "$": "\$",
              }
              text = "".join([replacements.get(c, c) for c in text])
          
          
          def h(text):
              text = text.replace('\\', r'\\')
              text = text.replace('`', r'\`')
              text = text.replace('*', r'\*')
              text = text.replace('_', r'\_')
              text = text.replace('{', r'\{')
              text = text.replace('}', r'\}')
              text = text.replace('[', r'\[')
              text = text.replace(']', r'\]')
              text = text.replace('(', r'\(')
              text = text.replace(')', r'\)')
              text = text.replace('>', r'\>')
              text = text.replace('#', r'\#')
              text = text.replace('+', r'\+')
              text = text.replace('-', r'\-')
              text = text.replace('.', r'\.')
              text = text.replace('!', r'\!')
              text = text.replace('$', r'\$')
          
          
          def i(text):
              text = text.replace('\\', r'\\').replace('`', r'\`').replace('*', r'\*').replace('_', r'\_').replace('{', r'\{').replace('}', r'\}').replace('[', r'\[').replace(']', r'\]').replace('(', r'\(').replace(')', r'\)').replace('>', r'\>').replace('#', r'\#').replace('+', r'\+').replace('-', r'\-').replace('.', r'\.').replace('!', r'\!').replace('$', r'\$')
          

          这是相同输入字符串 abc&def#ghi 的结果:

          • a) 100000 次循环,3 次中的最佳:每个循环 6.72 微秒
          • b) 100000 次循环,最好的 3 次:每个循环 2.64 μs
          • c) 100000 次循环,3 次中的最佳:每个循环 11.9 微秒
          • d) 100000 次循环,3 次中的最佳:每个循环 4.92 μs
          • e) 100000 次循环,最好的 3 次:每个循环 2.96 μs
          • f) 100000 次循环,3 次中的最佳:每个循环 4.29 微秒
          • g) 100000 次循环,3 次中的最佳:每个循环 4.68 微秒
          • h) 100000 次循环,3 次中的最佳:每个循环 4.73 μs
          • i) 100000 次循环,3 次中的最佳:每个循环 4.24 μs

          并且输入字符串更长(## *Something* and [another] thing in a longer sentence with {more} things to replace$):

          • a) 100000 次循环,3 次中的最佳:每个循环 7.59 微秒
          • b) 100000 次循环,3 次中的最佳:每个循环 6.54 微秒
          • c) 100000 次循环,3 次中的最佳:每个循环 16.9 μs
          • d) 100000 次循环,最好的 3 次:每个循环 7.29 μs
          • e) 100000 次循环,3 次中的最佳:每个循环 12.2 μs
          • f) 100000 次循环,最好的 3 次:每个循环 5.38 μs
          • g) 10000 次循环,3 次中的最佳:每个循环 21.7 μs
          • h) 100000 次循环,最好的 3 次:每个循环 5.7 μs
          • i) 100000 次循环,最好的 3 次:每个循环 5.13 μs

          添加几个变体:

          def ab(text):
              for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','$','\'']:
                  text = text.replace(ch,"\\"+ch)
          
          
          def ba(text):
              chars = "\\`*_{}[]()>#+-.!$"
              for c in chars:
                  if c in text:
                      text = text.replace(c, "\\" + c)
          

          使用较短的输入:

          • ab) 100000 次循环,3 次中的最佳:每个循环 7.05 μs
          • ba) 100000 次循环,3 次中的最佳:每个循环 2.4 μs

          较长的输入:

          • ab) 100000 次循环,3 次中的最佳:每个循环 7.71 微秒
          • ba) 100000 次循环,3 次中的最佳:每个循环 6.08 微秒

          所以我将使用ba 来提高可读性和速度。

          附录

          在 cmets 中的黑客提示下,abba 之间的一个区别是 if c in text: 检查。让我们针对另外两个变体对它们进行测试:

          def ab_with_check(text):
              for ch in ['\\','`','*','_','{','}','[',']','(',')','>','#','+','-','.','!','$','\'']:
                  if ch in text:
                      text = text.replace(ch,"\\"+ch)
          
          def ba_without_check(text):
              chars = "\\`*_{}[]()>#+-.!$"
              for c in chars:
                  text = text.replace(c, "\\" + c)
          

          在 Python 2.7.14 和 3.6.3 上以及在与早期设置不同的机器上,每个循环的时间(以 μs 为单位),因此无法直接比较。

          ╭────────────╥──────┬───────────────┬──────┬──────────────────╮
          │ Py, input  ║  ab  │ ab_with_check │  ba  │ ba_without_check │
          ╞════════════╬══════╪═══════════════╪══════╪══════════════════╡
          │ Py2, short ║ 8.81 │    4.22       │ 3.45 │    8.01          │
          │ Py3, short ║ 5.54 │    1.34       │ 1.46 │    5.34          │
          ├────────────╫──────┼───────────────┼──────┼──────────────────┤
          │ Py2, long  ║ 9.3  │    7.15       │ 6.85 │    8.55          │
          │ Py3, long  ║ 7.43 │    4.38       │ 4.41 │    7.02          │
          └────────────╨──────┴───────────────┴──────┴──────────────────┘
          

          我们可以得出结论:

          • 有支票的人比没有支票的人快 4 倍

          • ab_with_check 在 Python 3 上略微领先,但ba(带检查)在 Python 2 上的领先优势更大

          • 但是,最大的教训是 Python 3 比 Python 2 快 3 倍! Python 3 上最慢的速度和 Python 2 上最快的速度之间并没有太大的区别!

          【讨论】:

          • 为什么这不是例外答案?
          • @雨果;我认为这种时间差异是因为replace 仅在text 中找到c 时才调用ba,而在ab 的每次迭代中都会调用它。
          • @hacks 谢谢,我已经用更多时间更新了我的答案:添加检查对两者都更好,但最大的教训是 Python 3 的速度提高了 3 倍!
          • 你先生是个真正的英雄
          【解决方案7】:

          仅供参考,这对 OP 几乎没有用处,但它可能对其他读者有用(请不要投反对票,我知道这一点)。

          作为一个有点荒谬但有趣的练习,想看看我是否可以使用 python 函数式编程来替换多个字符。我很确定这不会比两次调用 replace() 更好。如果性能是一个问题,你可以在 rust、C、julia、perl、java、javascript 甚至 awk 中轻松击败它。它使用名为 pytoolz 的外部“帮助程序”包,通过 cython (cytoolz, it's a pypi package) 加速。

          from cytoolz.functoolz import compose
          from cytoolz.itertoolz import chain,sliding_window
          from itertools import starmap,imap,ifilter
          from operator import itemgetter,contains
          text='&hello#hi&yo&'
          char_index_iter=compose(partial(imap, itemgetter(0)), partial(ifilter, compose(partial(contains, '#&'), itemgetter(1))), enumerate)
          print '\\'.join(imap(text.__getitem__, starmap(slice, sliding_window(2, chain((0,), char_index_iter(text), (len(text),))))))
          

          我什至不打算解释这一点,因为没有人会费心使用它来完成多个替换。尽管如此,我还是觉得这样做有点成就感,并认为它可能会激励其他读者或赢得代码混淆竞赛。

          【讨论】:

          【解决方案8】:

          使用在 python2.7 和 python3.* 中可用的 reduce,您可以轻松地以干净和 python 的方式替换多个子字符串。

          # Lets define a helper method to make it easy to use
          def replacer(text, replacements):
              return reduce(
                  lambda text, ptuple: text.replace(ptuple[0], ptuple[1]), 
                  replacements, text
              )
          
          if __name__ == '__main__':
              uncleaned_str = "abc&def#ghi"
              cleaned_str = replacer(uncleaned_str, [("&","\&"),("#","\#")])
              print(cleaned_str) # "abc\&def\#ghi"
          

          在 python2.7 中你不必导入 reduce,但在 python3.* 中你必须从 functools 模块中导入它。

          【讨论】:

          • 添加'if'条件(Hugo提到的变体ba):lambda text, ptuple: text.replace(ptuple[0], ptuple[1]) if ptuple[0] in text else text
          【解决方案9】:

          这是一个使用str.translatestr.maketrans的python3方法:

          s = "abc&def#ghi"
          print(s.translate(str.maketrans({'&': '\&', '#': '\#'})))
          

          打印出来的字符串是abc\&def\#ghi

          【讨论】:

          • 这是一个很好的答案,但实际上执行一个 .translate() 似乎比三个链接的 .replace() 慢(使用 CPython 3.6.4)。
          • @Changaco 感谢您安排时间 ? 实际上我自己会使用replace(),但为了完整起见,我添加了这个答案。
          • 对于大字符串和许多替换,这应该会更快,尽管一些测试会很好......
          • 嗯,它不在我的机器上(2 和 17 替换相同)。
          • 此方法允许执行链式版本不能执行的“破坏性替换”。例如,将“a”替换为“b”,将“b”替换为“a”。
          【解决方案10】:

          聚会迟到了,但我在这个问题上浪费了很多时间,直到找到答案。

          短小精悍,translate优于replace。如果您对随时间优化的功能更感兴趣,请不要使用replace

          如果您不知道要替换的字符集是否与要替换的字符集重叠,也可以使用translate

          例子:

          使用replace 你会天真地期望sn-p "1234".replace("1", "2").replace("2", "3").replace("3", "4") 返回"2344",但实际上它会返回"4444"

          翻译似乎执行了 OP 最初想要的。

          【讨论】:

            【解决方案11】:

            也许是一个用于替换字符的简单循环:

            a = '&#'
            
            to_replace = ['&', '#']
            
            for char in to_replace:
                a = a.replace(char, "\\"+char)
            
            print(a)
            
            >>> \&\#
            

            【讨论】:

              【解决方案12】:

              这个怎么样?

              def replace_all(dict, str):
                  for key in dict:
                      str = str.replace(key, dict[key])
                  return str
              

              然后

              print(replace_all({"&":"\&", "#":"\#"}, "&#"))
              

              输出

              \&\#
              

              类似于answer

              【讨论】:

                【解决方案13】:

                使用正则表达式的高级方法

                import re
                text = "hello ,world!"
                replaces = {"hello": "hi", "world":" 2020", "!":"."}
                regex = re.sub("|".join(replaces.keys()), lambda match: replaces[match.string[match.start():match.end()]], text)
                print(regex)
                

                【讨论】:

                  【解决方案14】:

                  对于 Python 3.8 及以上版本,可以使用赋值表达式

                  [text := text.replace(s, f"\\{s}") for s in "&#" if s in text];
                  

                  虽然,我很不确定这是否会被视为“适当使用”PEP 572 中描述的赋值表达式,但看起来很干净并且读起来很好(在我看来)。如果你在 REPL 中运行,最后的分号会禁止输出。

                  如果您还想要所有中间字符串,这将是“合适的”。例如,(删除所有小写元音):

                  text = "Lorem ipsum dolor sit amet"
                  intermediates = [text := text.replace(i, "") for i in "aeiou" if i in text]
                  
                  ['Lorem ipsum dolor sit met',
                   'Lorm ipsum dolor sit mt',
                   'Lorm psum dolor st mt',
                   'Lrm psum dlr st mt',
                   'Lrm psm dlr st mt']
                  

                  从好的方面来说,它似乎(出乎意料地?)比接受答案中的一些更快的方法更快,并且似乎在增加字符串长度和增加替换数量的情况下表现良好。

                  上述比较的代码如下。我正在使用随机字符串让我的生活更简单一些,并且要替换的字符是从字符串本身中随机选择的。 (注意:我在这里使用了 ipython 的 %timeit 魔法,所以在 ipython/jupyter 中运行它)。

                  import random, string
                  
                  def make_txt(length):
                      "makes a random string of a given length"
                      return "".join(random.choices(string.printable, k=length))
                  
                  def get_substring(s, num):
                      "gets a substring"
                      return "".join(random.choices(s, k=num))
                  
                  def a(text, replace): # one of the better performing approaches from the accepted answer
                      for i in replace:
                          if i in text:
                               text = text.replace(i, "")
                  
                  def b(text, replace):
                      _ = (text := text.replace(i, "") for i in replace if i in text) 
                  
                  
                  def compare(strlen, replace_length):
                      "use ipython / jupyter for the %timeit functionality"
                  
                      times_a, times_b = [], []
                  
                      for i in range(*strlen):
                          el = make_txt(i)
                          et = get_substring(el, replace_length)
                  
                          res_a = %timeit -n 1000 -o a(el, et) # ipython magic
                  
                          el = make_txt(i)
                          et = get_substring(el, replace_length)
                          
                          res_b = %timeit -n 1000 -o b(el, et) # ipython magic
                  
                          times_a.append(res_a.average * 1e6)
                          times_b.append(res_b.average * 1e6)
                          
                      return times_a, times_b
                  
                  #----run
                  t2 = compare((2*2, 1000, 50), 2)
                  t10 = compare((2*10, 1000, 50), 10)
                  

                  【讨论】:

                    【解决方案15】:

                    这将帮助寻找简单解决方案的人。

                    def replacemany(our_str, to_be_replaced:tuple, replace_with:str):
                        for nextchar in to_be_replaced:
                            our_str = our_str.replace(nextchar, replace_with)
                        return our_str
                    
                    os = 'the rain in spain falls mainly on the plain ttttttttt sssssssssss nnnnnnnnnn'
                    tbr = ('a','t','s','n')
                    rw = ''
                    
                    print(replacemany(os,tbr,rw))
                    

                    输出:

                    he ri i pi fll mily o he pli

                    【讨论】:

                      猜你喜欢
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-10-16
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 1970-01-01
                      • 2011-06-17
                      • 2015-10-09
                      相关资源
                      最近更新 更多