美文网首页
字符串方法0x05 -- 字符映射

字符串方法0x05 -- 字符映射

作者: import_hello | 来源:发表于2018-11-24 12:53 被阅读0次

转载须注明出处:简书@Orca_J35 | GitHub@orca-j35

字符串不仅支持所有通用序列操作,还实现了很多附件方法。
我会以『字符串方法』为标题,分几篇笔记逐一介绍这些方法。
我会在这仓库中持续更新笔记:https://github.com/orca-j35/python_notes

translate

🔨 str.translate(table)

Return a copy of the string in which each character has been mapped through the given translation table. The table must be an object that implements indexing via __getitem__(), typically a mapping or sequence. When indexed by a Unicode ordinal (an integer), the table object can do any of the following: return a Unicode ordinal or a string, to map the character to one or more other characters; return None, to delete the character from the return string; or raise a LookupError exception, to map the character to itself.

# 该方法会按照table将字符串中的字符映射为指定的字符或字符串
# 在table中需使用码点作为键或索引
>>> table = {ord('a'): '#', ord('b'): '$$'}
>>> 'abcdabcd'.translate(table)
'#$$cd#$$cd'
# 在table中值为None,将被删除
>>> table = {ord('a'): '#', ord('b'): '$$',ord('c'):None}
>>> 'abcdabcd'.translate(table)
'#$$d#$$d'

You can use str.maketrans() to create a translation map from character-to-character mappings in different formats.

See also the codecs module for a more flexible approach to custom character mappings.
如果想要了解更多自定义字符映射的方法,可以查看 codecs 模块。

maketrans

🔨 static str.maketrans(x[, y[, z]])

This static method returns a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters (strings of length 1) to Unicode ordinals, strings (of arbitrary lengths) or None. Character keys will then be converted to ordinals.

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. If there is a third argument, it must be a string, whose characters will be mapped to Nonein the result.

# 生成一个共str.translate()使用的table
# 如果使用单参数形式,则x必须是一个字典,
# 该字典的键是Unicode码点或单个字符;该字典的值是码点或字符串或None,
# 如果键是字符,str.maketrans最终也会将其转换为码点
>>> d={97:'!','b':'##','c':None} # a的码点是97
>>> str.maketrans(d)
{97: '!', 98: '##', 99: None}
>>> 'abc'.translate(str.maketrans(d))
'!##'
# 如果使用双参数形式,则x和y必须是长度相同的字符串,
# x中的字符被逐一用作table字典的键,y中的字符被逐一用作table字典的值
>>> str.maketrans('abc','!@#')
{97: 33, 98: 64, 99: 35}
>>> 'abc'.translate(str.maketrans('abc','!@#'))
'!@#'
# 如果使用三参数形式,x和y与双参数的含义与双参数形式相同;
# z必须是一个字符串,该字符串中的字符会被映射为None,值为None的字符将被删除
>>> str.maketrans('abc','!@#','cde')
{97: 33, 98: 64, 99: None, 100: None, 101: None}
>>> 'abcdefg'.translate(str.maketrans('abc','!@#','cde'))
'!@fg'

相关文章

  • 字符串方法0x05 -- 字符映射

    转载须注明出处:简书@Orca_J35 | GitHub@orca-j35 字符串不仅支持所有通用序列操作,还实现...

  • Python第七天

    字符串常用方法 1、.maketrans()用来生成字符映射表, translate()根据字符映射表的对应关系转...

  • ES6的语法笔记

    参考 遍历 forEach 过滤 filter 映射 map 汇总reduce 构造函数 数组方法 字符串方法 展...

  • Python拾珍:2. 列表理解

    下面的函数接收一个字符串列表,将每个元素通过字符串方法 capitalize 进行映射,并返回一个新的字符串列表:...

  • 03-Nextflow脚本

    语言基础 Hello world、变量、列表、映射、多重赋值、条件执行、字符串、字符串插值、多行字符串 内置变量 ...

  • Mybatis基础

    是什么 数据持久层框架 使用方法 映射方式 1 xml映射 {}是参数标记 ${}是字符串替换的占位符(这种方式要...

  • 【golang】小技巧-利用fmt.Sscan完成字符串与变量映

    映射字符串的信息到变量中

  • iOS - 字符串方法

    删除字符串中的空格 用系统替换字符串方法 字符串分割 字符串分割方法 一字符串是否包含另一字符串 判断方法 大写转...

  • String 常用方法汇总

    截取字符串 字符串替换 字符串拼接 Stringbuilder 方法 StringJoiner 方法 setEmp...

  • 字符串方法

    #字符串调用字符串方法修改的话,原来的字符串不变,如果用字符串方法修改了字符串,需要重新赋值 currStr ="...

网友评论

      本文标题:字符串方法0x05 -- 字符映射

      本文链接:https://www.haomeiwen.com/subject/tvlqqqtx.html