Python 根据映射字典在字符串中查找和替换
Python find and replace in string based on a mapping dictionary
我有一个字符串
str = "abcdef"
和映射
map = {'a':'b','b':'c', 'c':'d'}
预期输出是
out_str = 'bcddef'
如何在 python 中的一次迭代中应用映射?
尝试:
s = "abcdef"
m = {"a": "b", "b": "c", "c": "d"}
print("".join(m.get(ch, ch) for ch in s))
打印:
bcddef
我有一个字符串
str = "abcdef"
和映射
map = {'a':'b','b':'c', 'c':'d'}
预期输出是
out_str = 'bcddef'
如何在 python 中的一次迭代中应用映射?
尝试:
s = "abcdef"
m = {"a": "b", "b": "c", "c": "d"}
print("".join(m.get(ch, ch) for ch in s))
打印:
bcddef