Eva*_*van 2 python substitution python-2.7
我有两个清单
list1 = ['a', 'b', 'c']
list2 = ['d', 'e', 'f']
Run Code Online (Sandbox Code Playgroud)
我有一个带有随机文本的变量.
var = 'backout'
我想将list1中存在的变量中的所有字母转换为list2中的字母.
expectedOutput = 'edfkout'
有没有办法做到这一点?
你想使用str.translate翻译表string.maketrans(这是str.maketrans在Python 3中)
from string import maketrans
s1 = 'abc'
s2 = 'def'
table = maketrans(s1, s2)
print('backout'.translate(table))
Run Code Online (Sandbox Code Playgroud)
编辑:
请注意,我们必须使用字符串而不是列表作为我们的参数maketrans.
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |