Python字符串translate()

本文深入讲解了Python字符串translate函数的用法,包括如何使用maketrans函数创建转换表,以及如何通过不同数量的参数实现字符替换和删除。文章提供了丰富的示例代码,展示了translate函数在实际编程中的应用。

Python String translate() function returns a new string with each character in the string replaced using the given translation table.

Python字符串translate()函数返回一个新字符串,该字符串中的每个字符都使用给定的转换表替换。

Python字符串translate() (Python String translate())

The translation table must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None.

转换表必须是Unicode常规到Unicode常规,字符串或无的映射。

We can create a translation table using maketrans() function or provide it manually using a dictionary mapping.

我们可以使用maketrans()函数创建翻译表,也可以使用字典映射手动提供它。

We can pass maximum three string arguments to maketrans() function.

我们最多可以将三个字符串参数传递给maketrans()函数。

  • If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None.

    如果只有一个参数,则它必须是将Unicode序数(整数)或字符映射到Unicode序数,字符串或无的字典。
  • If there are two arguments, they must be strings of equal length, and in the resulting dictionary, each character in x will be mapped to the character at the same position in y.

    如果有两个参数,则它们必须是长度相等的字符串,并且在结果字典中,x中的每个字符都将映射到y中相同位置的字符。
  • If there is a third argument, it must be a string, whose characters will be mapped to None in the result.

    如果有第三个参数,则它必须是一个字符串,其字符将在结果中映射为None。

Python字符串translate()示例 (Python String translate() Examples)

Let’s look at some examples of using string translate() function.

让我们看一些使用字符串translate()函数的示例。

带有一个参数的maketrans() (maketrans() with one argument)

s = 'ABCDBCA'

translation = s.maketrans({ord('A'): 'a', ord('B'): ord('b')})  # single argument as dict
print(s.translate(translation))

Output: abCDbCa

输出: abCDbCa

Here ‘A’ is being replaced with ‘a’ and ‘B is being replaced with ‘b’ in the result string.

结果字符串中的“ A”被替换为“ a”,而“ B”被替换为“ b”。

maketrans()有两个参数 (maketrans() with two arguments)

s = 'ABCDBCA'

translation = s.maketrans('A', 'a')

print(s.translate(translation))

translation = s.maketrans('ABCD', 'abcd')
print(s.translate(translation))

Output:

输出:

aBCDBCa
abcdbca

The first translation is replacing ‘A’ with ‘a’.

第一个翻译是将“ A”替换为“ a”。

The second translation has two string arguments of the same length and each character in the first string is being mapped with the corresponding index character in the second string. So A will be replaced with a, B will be replaced with b, C will be replaced with c and D will be replaced with d in the result string.

第二个转换具有两个相同长度的字符串参数,并且第一个字符串中的每个字符都与第二个字符串中的相应索引字符进行映射。 所以A将被替换aB将与被替换bC将与被替换cD将被替换d在结果字符串。

If the two argument strings are of different length, then an error will be raised.

如果两个参数字符串的长度不同,则会引发错误。

translation = s.maketrans('AB', 'a')

Error: ValueError: the first two maketrans arguments must have equal length

错误: ValueError:前两个maketrans参数的长度必须相等

带有三个参数的maketrans() (maketrans() with three arguments)

s = 'ABCDBCA'

translation = s.maketrans('AB', 'ab', 'ACD')
print(s.translate(translation))

Output: bb

输出: bb

Here ‘A’ is first being replaced by ‘a’ but then overridden to None because of third-string argument. Then ‘B’ is mapped with ‘b’. ‘C’ and ‘D’ characters are mapped to None for translation.

在这里,“ A”首先被“ a”替换,但是由于第三个字符串参数而被覆盖为None。 然后,“ B”被映射为“ b”。 “ C”和“ D”字符映射为“无”以进行翻译。

If we provide more than three arguments, then an error is raised.

如果我们提供三个以上的参数,则会引发错误。

translation = s.maketrans('AB', 'ab', 'CD', 'c')

Error: TypeError: maketrans() takes at most 3 arguments (4 given)

错误: TypeError: maketrans() takes at most 3 arguments (4 given)

带有手动映射的Python字符串translate() (Python String translate() with manual mapping)

s = 'ABCDBCA'

print(s.translate({ord('A'): ord('a'), ord('B'): ord('b'), ord('C'): None}))
print(s.translate({ord('A'): 'X', ord('B'): 'YZ', ord('C'): None}))

Output:

输出:

abDba
XYZDYZX

Notice that in the second statement ‘B’ is being replaced by the string ‘YZ’. Other mappings are a simple character to character replacement. I am using ord() function to provide the Unicode code point for the translation mappings.

请注意,在第二个语句中,“ B”被字符串“ YZ”替换。 其他映射是简单的字符到字符替换。 我正在使用ord()函数为翻译映射提供Unicode代码点。

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23697/python-string-translate

内容概要:本文系统梳理了多个科研领域的前沿研究与技术实现,重点涵盖FDTD方法中的完美匹配层(PML)研究,以及Matlab/Simulink在电磁、电力、控制、通信、信号处理、图像处理、路径规划、能源系统优化等领域的仿真与算法实现。文中列举了大量基于Matlab和Python的科研案例,如风电功率预测、负荷预测、无人机三维路径规划、电池系统故障诊断、雷达模拟、通信编码、微电网优化调度等,强调结合智能优化算法(如粒子群、遗传算法、深度学习等)提升系统性能。同时,提供了丰富的代码资源与仿真模型,涵盖永磁同步电机控制、逆变器设计、多智能体任务配、虚拟电厂调度等复杂系统,助力科研人员快速开展复现实验与创新研究。; 适合人群:具备一定编程基础,熟悉Matlab/Python工具,从事电气工程、自动化、通信、人工智能、新能源、控制科学等相关领域研究的研发人员及研究生。; 使用场景及目标:① 学习实现FDTD仿真中的PML边界条件以有效抑制数值反射;② 掌握Matlab/Simulink在多物理场建模、控制系统设计与优化算法中的综合应用;③ 借助提供的代码资源完成科研复现、课程设计、竞赛项目或工程原型开发; 阅读建议:此资源以科研实战为导向,不仅提供理论方法,更强调代码实现与仿真验证。建议读者结合自身研究方向,按目录顺序查阅相关模块,下载配套代码进行调试与二次开发,以达到学以致用、融会贯通的目的。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值