我们绘制地图,然后逐字翻译。当对字典使用 get 时,第二个参数指定如果找不到则返回什么。>>> trans = dict(zip(list("xyz"),list("abc")))>>> trans{'x': 'a', 'y': 'b', 'z': 'c'}>>> "".join([trans.get(i,i) for i in "hello xdxn"])'hello adan'>>>或者更改 trans 中的顺序以朝其他方向走>>> trans = dict(zip(list("abc"),list("xyz")))>>> trans{'a': 'x', 'b': 'y', 'c': 'z'}>>> "".join([trans.get(i,i) for i in "hello adan"])'hello xdxn'>>>