Python字符串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

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值