在Python for循环中使用String.Replace
我想使用字符串。替换使用for循环。这是我的代码:在Python for循环中使用String.Replace,python,string,replace,Python,String,Replace,我想使用字符串。替换使用for循环。这是我的代码: new = ['p','q','r'] my_str = 'there are two much person a, person b, person c.' old = ['a','b','c'] for i in range(0,len(old)): my_str = string.replace(my_str,old[i],new[i]) print(my_str) 但这给了我一个错误: TypeError:“str”对象不
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
for i in range(0,len(old)):
my_str = string.replace(my_str,old[i],new[i])
print(my_str)
但这给了我一个错误:
TypeError:“str”对象不能解释为整数
期望输出:
有两个多人p,q,r
这只是一个例子,我想为10000长度的列表运行一个for循环 试试看
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
for i in range(len(old)):
my_str = my_str.replace(old[i],new[i])
print(my_str)
但这可能不是很快
如果旧版本中的条目都是字母,则只有您可以这样做
import re
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
word=re.compile(r"\w*") # word characters
old_new=dict(zip(old,new))
ong=old_new.get
my_str=word.sub((lambda s:ong(s,s)),my_str)
print(my_str)
如果一个条目既有旧的也有新的(在较短的解决方案中没有避免),这也可以避免双重替换问题。Try
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
for i in range(len(old)):
my_str = my_str.replace(old[i],new[i])
print(my_str)
但这可能不是很快
如果旧版本中的条目都是字母,则只有您可以这样做
import re
new = ['p','q','r']
my_str = 'there are two much person a, person b, person c.'
old = ['a','b','c']
word=re.compile(r"\w*") # word characters
old_new=dict(zip(old,new))
ong=old_new.get
my_str=word.sub((lambda s:ong(s,s)),my_str)
print(my_str)
如果一个条目既有旧的也有新的(在较短的解决方案中没有避免),这也可以避免双重替换问题。string.replace()在Python2.x上可用,但在Python3.x中不推荐使用
下面是如何在Python3.x中使用它
str.replace(旧的,新的[,计数])
返回一个字符串的副本,其中所有出现的子字符串old都替换为new。如果给定了可选参数计数,则仅替换第一次出现的计数
在您的例子中,它是my_str.replace(旧的[index],新的[index])
string.replace()
在Python2.x上可用,但在Python3.x中不推荐使用
下面是如何在Python3.x中使用它
str.replace(旧的,新的[,计数])
返回一个字符串的副本,其中所有出现的子字符串old都替换为new。如果给定了可选参数计数,则仅替换第一次出现的计数
在你的情况下,它是我的替换(旧[索引]、新[索引])事实上,我无法重现你的问题;您的代码在Python 2.7上运行良好。然而,有更好的方法可以做到这一点。首先,您不必使用
范围
,而可以zip
查看旧的
和新的
列表:
for i in range(0,len(old)):
my_str = string.replace(my_str,old[i],new[i])
但是,这仍将替换are
中的a
和much
中的c
,并且它还可能替换先前替换中引入的字符,这可能不是您想要的。相反,您可以使用模块,将要替换为
的字符串连接到正则表达式,并使用\b
单词边界字符对其进行分隔,例如在您的情况下使用\b(a | b | c)\b
,然后使用字典查找正确的替换
d = dict(zip(old, new))
p = r'\b(' + '|'.join(old) + r')\b'
my_str = re.sub(p, lambda m: d.get(m.group()), my_str)
结果:
有两个多人p,q,r。
事实上,我无法重现你的问题;您的代码在Python 2.7上运行良好。然而,有更好的方法可以做到这一点。首先,您不必使用范围
,而可以zip
查看旧的
和新的
列表:
for i in range(0,len(old)):
my_str = string.replace(my_str,old[i],new[i])
但是,这仍将替换are
中的a
和much
中的c
,并且它还可能替换先前替换中引入的字符,这可能不是您想要的。相反,您可以使用模块,将要替换为
的字符串连接到正则表达式,并使用\b
单词边界字符对其进行分隔,例如在您的情况下使用\b(a | b | c)\b
,然后使用字典查找正确的替换
d = dict(zip(old, new))
p = r'\b(' + '|'.join(old) + r')\b'
my_str = re.sub(p, lambda m: d.get(m.group()), my_str)
结果:
有两个多人p,人q,人r。
不能复制。您的代码在Python 2.7上运行良好,string.replace
函数已经被弃用很长时间了。你应该改用str.replace
方法。你知道,即使你解决了任何奇怪的问题,你仍然要替换are
中的a
和much
中的c
。但是它对这个例子不起作用,我已经在答案部分写了。根据你的更新,你已经删除了它,问题是“年”中的“Y”是大写,而“年”中的“Y”是小写。下一次,发布一个。无法复制。您的代码在Python 2.7上运行良好,string.replace
函数已经被弃用很长时间了。你应该改用str.replace
方法。你知道,即使你解决了任何奇怪的问题,你仍然要替换are
中的a
和much
中的c
。但是它对这个例子不起作用,我已经在答案部分写了。根据你的更新,你已经删除了它,问题是“年”中的“Y”是大写,而“年”中的“Y”是小写。下一次,发布一个。与我的解决方案类似,只有在旧版中的条目仅包含单词字符时,此方法才有效。如果len(旧版)
为10000,则join(旧版)可能不是一个好主意。奥托,OP可能有XY问题…@PM2Ring很好,从这个角度来看,詹布罗尔的解决方案可能会更好+1与我的解决方案类似,这仅在旧版中的条目仅包含单词字符时有效。如果len(old)
为10000,则join(old)可能不是一个好主意。奥托,OP可能有XY问题…@PM2Ring很好,从这个角度来看,詹布罗尔的解决方案可能会更好+1.