首页 > 解决方案 > 使用替换字符串数组

问题描述

我想做以下事情:

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

然后输出应该是 hleo 磨损的,但似乎我不允许使用替换数组作为输入。有没有办法仍然这样做?

标签: pythonstr-replace

解决方案


这将起作用:

newthing = thing
for a,b in zip(A, B):
    newthing = newthing.replace(a, b)

推荐阅读