python - 使用替换字符串数组
问题描述
我想做以下事情:
thing = "hello world"
A = ["e","l"]
B = ["l","e"]
newthing = thing.replace(A,B)
print(newthing)
然后输出应该是 hleo 磨损的,但似乎我不允许使用替换数组作为输入。有没有办法仍然这样做?
解决方案
这将起作用:
newthing = thing
for a,b in zip(A, B):
newthing = newthing.replace(a, b)
推荐阅读
- mongodb - php mongodb中的投影
- javascript - 如何使用迭代器嵌套循环?
- node.js - golang base64 编码与 nodejs 缓冲区 base64 编码
- php - PHP 分页集成脚本在输出中显示没有页面
- r - 逐行合并多个矩阵并将其存储在一个新矩阵中
- c# - 使用 jQuery.timepicker 仅在 ASP.NET MVC 中节省时间
- javascript - PHP从div中获取p元素
- javascript - 更改悬停时的图像会更改整行的图像
- node.js - 在最后一行之前添加值行并保留最后一行格式
- go - How to index entire GOPATH for existing project in Goland?