Python——maketrans和translate方法
2014-03-09 17:05
423 查看
maketrans和translate是密切相关的两个方法,先看translate的说明
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.
简单来说就是对字符串S移除deletechars包含的字符,然后保留下来的字符按照table里面的字符映射关系映射(比如a变成A,后面会解释到)。那个莫名其妙的"which must be a string of length 256"就不用深究了,反正table就是由string.maketrans方法生成的,对于string.maketrans方法,这里有个更清晰的解释,如下:
string.maketrans(intab, outtab) --> This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab
and outtab must have the same length.
下面是一些实例说明:
import string
s = 'abcdefg-1234567'
table = string.maketrans('', '') #没有映射,实际上就是按原始字符保留,看下面用到translate中时的效果
s.translate(table) # 输出abcdefg-1234567
s.translate(table, 'abc123') #输出defg-4567 可以看到删除了字符abc123
#下面再看有字符映射时的效果
table = string.maketrans('abc', 'ABC') #用于translate中时的效果如下
s.translate(table) #输出ABCdefg-1234567 就是将abc映射为大写的ABC,前提是abc如果被保留下来了
s.translate(table, 'ab123') #输出Cdefg-4567 先把s中的ab123去除了,然后在保留下来的字符中应用table中指定的字符映射关系映射:c -> C
解释完毕,现在知道为什么maketrans(intab, outtab)中两个字符串参数长度必须一样了,因为是字符一对一的映射。
神奇的Python中总是有很多奇怪的存在
S.translate(table [,deletechars]) -> string
Return a copy of the string S, where all characters occurring
in the optional argument deletechars are removed, and the
remaining characters have been mapped through the given
translation table, which must be a string of length 256.
简单来说就是对字符串S移除deletechars包含的字符,然后保留下来的字符按照table里面的字符映射关系映射(比如a变成A,后面会解释到)。那个莫名其妙的"which must be a string of length 256"就不用深究了,反正table就是由string.maketrans方法生成的,对于string.maketrans方法,这里有个更清晰的解释,如下:
string.maketrans(intab, outtab) --> This method returns a translation table that maps each character in the intab string into the character at the same position in the outtab string. Then this table is passed to the translate() function. Note that both intab
and outtab must have the same length.
下面是一些实例说明:
import string
s = 'abcdefg-1234567'
table = string.maketrans('', '') #没有映射,实际上就是按原始字符保留,看下面用到translate中时的效果
s.translate(table) # 输出abcdefg-1234567
s.translate(table, 'abc123') #输出defg-4567 可以看到删除了字符abc123
#下面再看有字符映射时的效果
table = string.maketrans('abc', 'ABC') #用于translate中时的效果如下
s.translate(table) #输出ABCdefg-1234567 就是将abc映射为大写的ABC,前提是abc如果被保留下来了
s.translate(table, 'ab123') #输出Cdefg-4567 先把s中的ab123去除了,然后在保留下来的字符中应用table中指定的字符映射关系映射:c -> C
解释完毕,现在知道为什么maketrans(intab, outtab)中两个字符串参数长度必须一样了,因为是字符一对一的映射。
神奇的Python中总是有很多奇怪的存在
相关文章推荐
- Python——maketrans和translate方法到底是什么玩意儿
- 闭包在python中的应用,translate和maketrans方法详解
- python---语句与数据结构应用,迭代、迭代字典、字典排序、字典根据键值查找键、sorted排序方法、translate与maketrans、with语句
- Python——maketrans和translate方法,可以用来从string中替换和删除字符
- Python——maketrans和translate方法,可以用来从string中替换和删除字符
- Python——maketrans和translate方法到底是什么玩意儿
- 二十、python 字符串去重,主要依赖maketrans和translate这两个方法
- Python——maketrans和translate方法,可以用来从string中替换和删除字符
- Python——maketrans和translate方法,可以用来从string中替换和删除字符
- Python——maketrans和translate方法
- 【python cookbook】简化字符串的translate 方法的使用
- 闭包在python中的应用之translate和maketrans用法详解
- Python2.x版本中maketrans()方法的使用介绍
- Python translate()方法
- Python translate()方法
- Python2.x版本中maketrans()方法的使用介绍
- translate和maketrans 方法总结
- python字符串英雄技能,title,join,ljust,lower,upper,lstrip,maketrans,translate,partition,split,splitlines
- python3.x和python2.x的translate与maketrans函数使用差异
- Python 3.x 字符串的makestrans函数和translate方法使用详解