使用替换为字符串的数组

问题描述 投票:0回答:1

我想执行以下操作:

thing = "hello world"
A = ["e","l"]
B = ["l","e"]
newthing = thing.replace(A,B)
print(newthing)

然后应该很高兴地输出了输出,但是似乎不允许使用数组替换作为输入。有没有办法像这样继续做呢?

python str-replace
1个回答
0
投票

这将起作用:

newthing = thing
for a,b in zip(A,B):
    newthing = newthing.replace(a,b)
© www.soinside.com 2019 - 2024. All rights reserved.