Python maketrans()是一個靜態(tài)函數(shù)(使用str類型直接調(diào)用,而非字符串),其作用是為str.translate()函數(shù)提供轉(zhuǎn)換字符映射表。
官方文檔給出的語法格式如下:
static str.maketrans(x[, y[, z]])
該函數(shù)有三個參數(shù),其中后兩個是可選參數(shù)。
(1)該函數(shù)只有一個參數(shù) x 時, x 必須是字典類型,以給出轉(zhuǎn)換的映射關(guān)系。字典的鍵取值可以是:對應(yīng)某個Unicode字符的整型序數(shù),或者字符(長度為1的字符串);鍵對應(yīng)的值取值可以為Unicode字符序數(shù)(整型),任意長度的字符串或None。
(2)該函數(shù)取兩個參數(shù)時, x 和 y 必須是等長的字符串。在生成的結(jié)果字典中, x 中的每一個字符將于 y 中相同位置的字符進行匹配;
(3)該函數(shù)取第三個參數(shù)時,該參數(shù) z 必須是一個字符串,字符串中的每個字符將使用 None 進行匹配,即在使用 str.translate()函數(shù) 對 str 進行轉(zhuǎn)換時, str 中包含在 z 中的字符將被刪除掉。
該函數(shù)的返回值是字典類型。是由 x 中的字符對應(yīng)的Unicode序數(shù)作為鍵, y 中對應(yīng)的字符序數(shù)作為元素值構(gòu)成的字典,當(dāng)指定 z 參數(shù)時, z 參數(shù)中的每個字符Unicode序數(shù)也會作為字典中的鍵,而對應(yīng)的值為 None .
當(dāng)給定一個參數(shù)x時,x必須是字典類型。
>>> d1 = str.maketrans({98:104})
>>> d1
{98: 104}
>>> type(d1)
<class 'dict'>
>>> d2 = str.maketrans({'a':'b'})
>>> d2
{97: 'b'}
>>> d3 = str.maketrans({'a':'abcd'})
>>> d3
{97: 'abcd'}
>>> d4 = str.maketrans({'a':None,'b':'c',100:110,112:'abc'})
>>> d4
{97: None, 98: 'c', 100: 110, 112: 'abc'}
>>> d5 = str.maketrans('a')
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
d5 = str.maketrans('a')
TypeError: if you give only one argument to maketrans it must be a dict
>>> d5 = str.maketrans(103)
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
d5 = str.maketrans(103)
TypeError: if you give only one argument to maketrans it must be a dict
>>> d5 = str.maketrans(None)
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
d5 = str.maketrans(None)
TypeError: if you give only one argument to maketrans it must be a dict
>>>
從上面的例子可以看出,當(dāng)所給的參數(shù)不是字典類型時,將引發(fā)“TypeError”錯誤。
當(dāng)給定兩個參數(shù)x和y時,x和y必須是等長的字符串。
>>> x = '1234'
>>> y = 'abcd'
>>> print(str.maketrans(x,y))
{49: 97, 50: 98, 51: 99, 52: 100}
>>> print(str.maketrans('xyz','ab'))
Traceback (most recent call last):
File "<pyshell#17>", line 1, in <module>
print(str.maketrans('xyz','ab'))
ValueError: the first two maketrans arguments must have equal length
>>> print(str.maketrans('ab','xyz'))
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
print(str.maketrans('ab','xyz'))
ValueError: the first two maketrans arguments must have equal length
>>> print(str.maketrans('abc','xy '))
{97: 120, 98: 121, 99: 32}
>>>
注意:最后一個例子中 y 參數(shù)的字符串中最后一個字符為空格。同時,我們注意到如果兩個字符串不等長時,將引發(fā)“ValueError”錯誤。
給定三個參數(shù)時,z參數(shù)是一個字符串,用于指定要刪除的字符。
>>> x = 'abcd'
>>> y = 'wxyz'
>>> z = 'hij'
>>> print(str.maketrans(x, y, z))
{97: 119, 98: 120, 99: 121, 100: 122, 104: None, 105: None, 106: None}
>>>
從上面的例子可以看出,'h', 'i', 'j' 三個字符被轉(zhuǎn)換成了None,即在使用str.translate()函數(shù)時,這些字符將被刪除掉。
前面說過,maketrans()函數(shù)主要是為translate()函數(shù)生成轉(zhuǎn)換的字符映射表,下面的例子將演示兩者如何配合使用的。
>>> x = 'aeiou'
>>> y = 'vwxyz'
>>> z = 'rst'
>>> dic = str.maketrans(x, y, z)
>>> str1 = 'I love my country as much as i love my life.'
>>> res = str1.translate(dic)
>>> print(res)
I lyvw my cyzny v mzch v x lyvw my lxfw.
>>>
上面演示了maketrans()函數(shù)和translate()函數(shù)的配合使用方法。從結(jié)果中可以看出,translate()函數(shù)執(zhí)行完畢后,str1中的字符'a', 'e', 'i', 'o', 'u' 五個字母(由參數(shù)x給定的字符串中的字符)分別被 'v', 'w', 'x', 'y' 和 'z' (由參數(shù)y給定的字符串中的字符)給替換掉了,同時在str1中刪除掉了參數(shù)z中指定的字符。
從這個例子中,我們也注意到,maketrans()函數(shù)和translate()函數(shù)的參數(shù)值是大小寫敏感的,原因在于其按照Unicode字符序數(shù)來處理字符。
以上講解了Python中靜態(tài)函數(shù)maketrans()的參數(shù)及使用方法,并給出了其與translate()函數(shù)配合使用的例子。
實際上,maketrans()函數(shù)就是生成由鍵值對構(gòu)成的字符映射表,其結(jié)果作為translate()函數(shù)的參數(shù),用于對字符串中的字符進行替換,maketrans()函數(shù)中x參數(shù)用于給出需要替換的字符構(gòu)成的字符串,y參數(shù)用于給出替換后的字符構(gòu)成的字符串,z參數(shù)給出需要在字符串刪除掉的字符構(gòu)成的字符串。
新聞熱點
疑難解答
圖片精選