[英]A list of string replacements in Python
Is there a far shorter way to write the following code?有没有更短的方法来编写以下代码?
my_string = my_string.replace('A', '1')
my_string = my_string.replace('B', '2')
my_string = my_string.replace('C', '3')
my_string = my_string.replace('D', '4')
my_string = my_string.replace('E', '5')
Note that I don't need those exact values replaced;请注意,我不需要替换那些确切的值; I'm simply looking for a way to turn 5+ lines into fewer than 5我只是在寻找一种将 5+ 行变成少于 5 行的方法
Looks like a good opportunity to use a loop:看起来是使用循环的好机会:
mapping = { 'A':'1', 'B':'2', 'C':'3', 'D':'4', 'E':'5'}
for k, v in mapping.iteritems():
my_string = my_string.replace(k, v)
A faster approach if you don't mind the parentheses would be:如果您不介意括号,则更快的方法是:
mapping = [ ('A', '1'), ('B', '2'), ('C', '3'), ('D', '4'), ('E', '5') ]
for k, v in mapping:
my_string = my_string.replace(k, v)
You can easily use string.maketrans() to create the mapping string to pass to str.translate():您可以轻松地使用 string.maketrans() 创建映射字符串以传递给 str.translate():
import string
trans = string.maketrans("ABCDE","12345")
my_string = my_string.translate(trans)
If you want to get the wrong answer, slowly, then use string.replace in a loop.如果你想得到错误的答案,慢慢地,然后在循环中使用 string.replace 。 (Though it does work in this case of no overlap among the patterns and replacements.) (尽管它在模式和替换之间没有重叠的情况下确实有效。)
For the general case with possible overlaps or a long subject string, use re.sub:对于可能重叠或主题字符串较长的一般情况,请使用 re.sub:
import re
def multisub(subs, subject):
"Simultaneously perform all substitutions on the subject string."
pattern = '|'.join('(%s)' % re.escape(p) for p, s in subs)
substs = [s for p, s in subs]
replace = lambda m: substs[m.lastindex - 1]
return re.sub(pattern, replace, subject)
>>> multisub([('hi', 'bye'), ('bye', 'hi')], 'hi and bye')
'bye and hi'
For the special case of 1-character patterns and 1- or 0-character replacements, use string.maketrans.对于 1 个字符模式和 1 个或 0 个字符替换的特殊情况,请使用 string.maketrans。
Also look into str.translate()
.还要查看str.translate()
。 It replaces characters according to a mapping you provide for Unicode strings, or otherwise must be told what to replace each character from chr(0) to chr(255) with.它根据您为 Unicode 字符串提供的映射替换字符,否则必须告诉用什么替换从 chr(0) 到 chr(255) 的每个字符。
replaceDict = {'A':'1','B':'2','C':'3','D':'4','E':'5'} for key, replacement in replaceDict.items(): my_string = my_string.replace( key, replacement )
I think it could be a little more efficient:我认为它可能会更有效率:
mapping = { 'A':'1', 'B':'2', 'C':'3', 'D':'4', 'E':'5'}
my_string = "".join([mapping[c] if c in mapping else c for c in my_string])
I suggest some benchmark with "timeit", with real cases in base of the lenght of "my_string".我建议使用“timeit”进行一些基准测试,以“my_string”的长度为基础的真实案例。
You can do it in one line using Pandas.您可以使用 Pandas 在一行中完成。
import pandas as pd
my_string="A B C test"
my_string =pd.DataFrame([my_string])[0].replace(["A","B","C","D","E"],['1','2','3','4','5'],regex=True)[0]
print(my_string)
'1 2 3 test'
One way I do it is with an associated array (a dictionary).我这样做的一种方法是使用关联的数组(字典)。 Here is an example of the replacements I use when getting a file ready for deployment in LaTeX using regular expressions.这是我使用正则表达式准备好在 LaTeX 中部署文件时使用的替换示例。
import re
def escapeTexString(string): # Returns TeX-friendly string
rep = { # define desired replacements in this dictionary (mapping)
'&': '\\&',
'%': '\\%',
'#': '\\#',
'_': '\\_',
'{': '\\{', # REGEX Special
'}': '\\}', # REGEX Special
'~': '\\char"007E{}', # LaTeX Special
'$': '\\$', # REGEX Special
'\\': '\\char"005C{}', # REGEX/LaTeX Special
'^': '\\char"005E{}', # REGEX/LaTeX Special
'"': '\\char"FF02{}'
}
# use these two lines to do the replacement (could be shortened to one line)
pattern = re.compile("|".join(map(re.escape,rep.keys()))) # Create single pattern object (key to simultaneous replacement)
new_string = pattern.sub(lambda match: rep[match.group(0)], string)
return new_string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.