maketrans(x, [y, [z]])

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.

라고 써져있는데.. 음.. 테이블맵에 맞춰서 변환해준다는 것같다. 테이블맵을 짠후에는 translate()함수를 사용하여, 문자열을 바꿔주는 함수.

 

Maketrans(table [,deletechars])

- translate()함수에 쓰일 번역용 맵을 반환

- 인자가 하나일 경우 사전형식 입력

- 인자가 둘인경우 길이가 같은 문자열을 입력받아 x의 문자가 y로 변환

- 인자가 셋인경우 x가 y로 변환되고 z문자가 None으로 대체(삭제)

 

Help on built-in function 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 or None.
    If the table argument is None, no translation is applied and
    the operation simply removes the characters in deletechars.

 

ex)

>>>a= "abcd"

>>>b=string.maketrans("abcd","efgt")

>>>c = a.translate(b)

>>>c

'efgt'

>>>

 

 

대부분의 사이트와 블로그에선 str.maketrans로 나오지만 내 우분투(11.10)파이썬(2.7.2)에서는 명령어가 먹히지 않았다. 대신 string.maketrans 를 사용해야함.

'Programming > Python' 카테고리의 다른 글

Python Challenge 4  (0) 2012.05.23
Python challenge 3  (1) 2012.05.23
Python challenge 2  (0) 2012.05.23
Python challenge 1  (0) 2012.05.15
Python challenge 0  (0) 2012.05.15

+ Recent posts