maketrans 和 translate的用法(配合使用)

下面是python的英文用法解释

maketrans(x, y=None, z=None, /)
Return a translation table usable for str.translate().

If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then 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 None in the result

S.translate(table) -> str

Return a copy of the string S, where all characters have been mapped
through the given translation table, which must be a mapping of
Unicode ordinals to Unicode ordinals, strings, or None.
Unmapped characters are left untouched. Characters mapped to None
are deleted.

makestans返回一个给translate用的映射表,translate根据映射表构造新的字符串。

makestran根据参数的个数有三种使用方法:

1)一个参数情况,参数必须是字典

一个字符转换成一个字符

 >>> a='qwerabc2348'
>>> d={'a':'A','q':'Q'} #转换映射表
>>> tans=str.maketrans(d) #转换为translate可以使用的映射表
>>> tans
{: 'A', : 'Q'} #translate可以使用的映射表
>>> a.translate(tans)
'QwerAbc2348' #转换后的结果

一个字符转换为多个字符

 >>> d2={'a':'*A*','q':'*Q*'}
>>> tans2=str.maketrans(d2)
>>> tans2
{: '*A*', : '*Q*'}
>>> a.translate(tans2)
'*Q*wer*A*bc2348

一个字符转换为None,效果为过滤删除字符

 >>> d3={'a':None,'q':None}
>>> tans3=str.maketrans(d3)
>>> tans3
{: None, : None}
>>> a.translate(tans3)
'werbc2348'

2)两个参数的情况,参数(字符串)必须长度相等。

 >>> a='acbsdwf124'
>>> tans4=str.makestrans('abc','ABC')
>>> tans4=str.maketrans('abc','ABC')
>>> tans4
{: , : , : }
>>> a.translate(tans4)
'ACBsdwf124'

3)三个参数的情况,前两个参数效果和2)相同,第三个参数为要过滤删除的字符表(第三个参数都映射为None)

 >>> a
'acbsdwf124'
>>> tans5=str.maketrans('abc','ABC','')
>>> tans5
{: , : , : , : None, : None, : None, : None}
>>> a.translate(tans5)
'ACBsdwf'

4)映射表中的数字为unicode编码数字

 >>> ord('a')

 >>> chr()
'a'

python3 字符串属性(三)的更多相关文章

  1. python3 字符串属性(一)

    python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...

  2. python3 字符串属性(四)

    1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...

  3. python3字符串属性(二)

    1.S.isdecimal() -> bool    Return True if there are only decimal characters in S, False otherwise ...

  4. 彻底了解构建 JSON 字符串的三种方式

    原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...

  5. python系列四:Python3字符串

    #!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...

  6. Python3字符串 详解

    Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...

  7. NSAttributedString字符串属性类

    //定义一个可变字符串属性对象aStr NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithStri ...

  8. 字符串属性使用strong的原因

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  9. mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符

    连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...

随机推荐

  1. Hadoop编码解码【压缩解压缩】机制具体解释(1)

    想想一下,当你须要处理500TB的数据的时候,你最先要做的是存储下来. 你是选择源文件存储呢?还是处理压缩再存储?非常显然,压缩编码处理是必须的.一段刚刚捕获的60分钟原始视屏可能达到2G,经过压缩处 ...

  2. Java线程面试题:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次

    package thread; /** * 需求:线程编程:子线程先运行 2 次,然后主线程运行 4 次,如此反复运行 3 次. * @author zhongfg * @date 2015-06-1 ...

  3. 安装PyTorch 0.4.0

    https://blog.csdn.net/sunqiande88/article/details/80085569 https://blog.csdn.net/xiangxianghehe/arti ...

  4. 单向HASH——MurmurHash

    //seed 是大质数unsigned long long MurmurHash64B ( const void * key, int len, unsigned int seed ) { const ...

  5. 线性同余方程模板( A+C*x=B(mod D) )

    void extendgcd(long long a,long long b,long long &d,long long &x,long long &y) { ){d=a;x ...

  6. android菜鸟学习笔记18----Android数据存储(二)SharedPreferences

    数据存储的方式,有比直接文件读写更加简便的方式,那就是操作SharedPreferences. SharedPreferences一般用于存储用户的偏好设定,暂时不支持多进程操作. SharedPre ...

  7. 九度OJ 1193:矩阵转置 (矩阵计算)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1673 解决:1132 题目描述: 输入一个N*N的矩阵,将其转置后输出.要求:不得使用任何数组(就地逆置). 输入: 输入的第一行包括一个 ...

  8. Spring @Configuration注解

    1 该注解的用途 这个注解表示这个类可以作为spring ioc容器bean的来源,其本质上它是对xml文件中创建bean的一种替换.有了这个注释,Spring framework就能在需要的时候构造 ...

  9. React-Native 安装改变镜像

    3.安装完node后建议设置npm镜像以加速后面的过程,否则后面插件安装巨慢 npm config set registry https://registry.npm.taobao.org --glo ...

  10. Redis通过PUBLISH / SUBSCRIBE 等命令实现了订阅与发布模式

    # 切换目录 [root@localhost /]# cd /opt/redis-4.0.10 # 启动客户端 -p 指定端口 [root@localhost ~]# redis-cli -p 638 ...