gpt4 book ai didi

python - 替换字符串中多个字符的最佳方法?

转载 作者:IT老高 更新时间:2023-10-28 12:29:13 34 4
gpt4 key购买 nike

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

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

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

最佳答案

替换两个字符

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

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

每个功能的时间安排:

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

以下是函数:

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 微秒
  • c) 100000 次循环,3 次中的最佳:每个循环 11.9 微秒
  • d) 100000 次循环,3 次中的最佳:每个循环 4.92 微秒
  • e) 100000 次循环,最好的 3 次:每个循环 2.96 微秒
  • f) 100000 次循环,3 次中的最佳:每个循环 4.29 微秒
  • g) 100000 次循环,最好的 3 次:每个循环 4.68 微秒
  • h) 100000 次循环,3 次中的最佳:每个循环 4.73 微秒
  • i) 100000 次循环,3 次中的最佳:每个循环 4.24 微秒

并且使用更长的输入字符串(## *Something* 和 [another] 的东西在更长的句子中,用 {more} 东西替换$):

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

添加几个变体:

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 微秒
  • ba) 100000 次循环,3 次中的最佳:每个循环 2.4 微秒

较长的输入:

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

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

附录

由于注释中的黑客提示,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 上最快的速度之间并没有太大的区别!

关于python - 替换字符串中多个字符的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3411771/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com