对于Python中的字符串替换列表感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解python字符串替换,并且为您提供关于php–在Python中执行多个字符串替换的最快实现、python–
对于Python中的字符串替换列表感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解python字符串 替换,并且为您提供关于php – 在Python中执行多个字符串替换的最快实现、python – 如何用下一个字符串替换字符串中的字符?、python – 通过位置列表将字符串中的字符替换为另一个字符、python – 错误的字符串替换版本的宝贵知识。
本文目录一览:- Python中的字符串替换列表(python字符串 替换)
- php – 在Python中执行多个字符串替换的最快实现
- python – 如何用下一个字符串替换字符串中的字符?
- python – 通过位置列表将字符串中的字符替换为另一个字符
- python – 错误的字符串替换版本
Python中的字符串替换列表(python字符串 替换)
有没有更短的方法可以编写以下代码?
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'')
注意,我不需要替换那些确切的值。我只是在寻找一种将5条以上的线变成少于5条线的方法
答案1
小编典典看起来是使用循环的好机会:
mapping = { ''A'':''1'', ''B'':''2'', ''C'':''3'', ''D'':''4'', ''E'':''5''}for k, v in mapping.iteritems(): my_string = my_string.replace(k, v)
如果您不介意括号,则更快的方法是:
mapping = [ (''A'', ''1''), (''B'', ''2''), (''C'', ''3''), (''D'', ''4''), (''E'', ''5'') ]for k, v in mapping: my_string = my_string.replace(k, v)
php – 在Python中执行多个字符串替换的最快实现
例如,你如何实现一个快速的函数,其行为类似于 Python中的PHP的htmlspecialchars?
我比较了(1)多个’替换’方法,(2)正则表达式方法,和(3)马特安德森的方法.
n = 10次运行,结果如下:
在100个字符上:
TIME: 0 ms [ replace_method(str) ] TIME: 5 ms [ regular_expression_method(str,dict) ] TIME: 1 ms [ matts_multi_replace_method(list,str) ]
在1000个字符上:
TIME: 0 ms [ replace_method(str) ] TIME: 3 ms [ regular_expression_method(str,dict) ] TIME: 2 ms [ matts_multi_replace_method(list,str) ]
在10000个字符上:
TIME: 3 ms [ replace_method(str) ] TIME: 7 ms [ regular_expression_method(str,dict) ] TIME: 5 ms [ matts_multi_replace_method(list,str) ]
在100000个字符:
TIME: 36 ms [ replace_method(str) ] TIME: 46 ms [ regular_expression_method(str,dict) ] TIME: 39 ms [ matts_multi_replace_method(list,str) ]
在1000000个字符上:
TIME: 318 ms [ replace_method(str) ] TIME: 360 ms [ regular_expression_method(str,dict) ] TIME: 320 ms [ matts_multi_replace_method(list,str) ]
在3687809字符:
TIME: 1.277524 sec [ replace_method(str) ] TIME: 1.290590 sec [ regular_expression_method(str,dict) ] TIME: 1.116601 sec [ matts_multi_replace_method(list,str) ]
因此,对Matt在相当大的输入字符串上击败多’替换’方法感到荣幸.
任何人都有想法在较小的字符串上击败它吗?
也许有点难以绕过下面的代码(这对我来说,我写了它),但似乎按预期运行.我没有对它进行基准测试,但我怀疑它会相当快.
def multi_replace(pairs,text): stack = list(pairs) stack.reverse() def replace(stack,parts): if not stack: return parts # copy the stack so I don't disturb parallel recursions stack = list(stack) from_,to = stack.pop() #print 'split (%r=>%r)' % (from_,to),parts split_parts = [replace(stack,part.split(from_)) for part in parts] parts = [to.join(split_subparts) for split_subparts in split_parts] #print 'join (%r=>%r)' % (from_,parts return parts return replace(stack,[text])[0] print multi_replace( [('foo','bar'),('baaz','foo'),('quux','moop')],'foobarbaazfooquuxquux')
对于:
barbarfoobarmoopmoop
python – 如何用下一个字符串替换字符串中的字符?
abcdefghijklmnopqrstuvwxyz
应成为:
bcdefghijklmnopqrstuvwxyza
是否可以在不使用替换功能的情况下进行26次?
解决方法
str.translate()
method让Python在一个步骤中替换其他字符.
使用string.maketrans()
function将ASCII字符映射到其目标;使用string.ascii_lowercase
可以帮助您,因为它可以节省您自己键入所有字母:
from string import ascii_lowercase try: # Python 2 from string import maketrans except ImportError: # Python 3 made maketrans a static method maketrans = str.maketrans cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1]) encrypted = text.translate(cipher_map)
演示:
>>> from string import maketrans >>> from string import ascii_lowercase >>> cipher_map = maketrans(ascii_lowercase,ascii_lowercase[1:] + ascii_lowercase[:1]) >>> text = ''the quick brown fox jumped over the lazy dog'' >>> text.translate(cipher_map) ''uif rvjdl cspxo gpy kvnqfe pwfs uif mbaz eph''
python – 通过位置列表将字符串中的字符替换为另一个字符
例如,我有字符串:string =“******** u”,列表:list = [0,2,4]和character =’a’.如何用?替换string [0],string [2],string [4]?
解决方法
s = "********u" l ={0,4} final_string = ''.join(["a" if i in l else a for i,a in enumerate(s)])
输出:
'a*a*a***u'
python – 错误的字符串替换版本
> How to replace two things at once in a string? 6个
> How to replace multiple substrings of a string? 19个
我陷入了一个非常直接的问题,但经过一些调整后,我根本找不到一个简单有效的算法来做到这一点.
所以基本上我有一个字符串,其中包含某些元素,例如:
l = "test %1,%13,%14,%15"
还有一张地图:
dict = {"%1": "%33","%13": "%14","%14","%15","%15": "%17"}
我想做以下的事情:
for k in dict.keys(): l = l.replace(k,dict[k])
所以我期待的是:
l = "test %33,%15,%17"
但显然这是不可行的,因为键和值之间存在一些冲突.所以上面的代码会输出:
l = "test %33,%17,%17"
很抱歉这样的原生问题,但我该如何解决这个并得到我预期的输出?请注意,每个键/值的长度不固定,所以我不能做类似的事情:
m_idx = l_copy.find(key) l = l[:m_idx] + dict[key] + l[m_idx+len(key):]
因为长度不固定,上面的代码仍然是错误的.
我清楚这个吗?任何建议将非常感激!
=======更新
因此所有键都遵循%[0-9]的这种模式.
解决方法
import re l = "test %1,%15" _dict = {"%1": "%33","%14":"%15","%15": "%17"} new_l = re.sub('%\d+',lambda x:_dict[x.group()],l)
输出:
'test %33,%17'
如果_dict中没有键,则可以使用dict.get通过提供默认值来防止KeyError:
new_l = re.sub('%\d+',lambda x:_dict.get(x.group(),x.group()),l)
我们今天的关于Python中的字符串替换列表和python字符串 替换的分享已经告一段落,感谢您的关注,如果您想了解更多关于php – 在Python中执行多个字符串替换的最快实现、python – 如何用下一个字符串替换字符串中的字符?、python – 通过位置列表将字符串中的字符替换为另一个字符、python – 错误的字符串替换版本的相关信息,请在本站查询。
本文标签: