Application of closures in python, explanation of the translate and maketrans methods, and pythonmaketrans

Source: Internet
Author: User

Application of closures in python, explanation of the translate and maketrans methods, and pythonmaketrans

Python is efficient in processing strings and has many methods. The maketrans and translate methods are used in many ways, but they often cannot be used.

Let's review the two methods:

1. s. translate (table, str) removes characters contained in str from string s. The remaining strings are replaced by the character ing relationship in table. A table can be interpreted as A conversion table. Compare 'a'-> 'A', 'B'-> 'B '.

2. tabel = string. maketrans ('s1 ', 's2') s1 and s2 must have the same length. maketrans generates a conversion table. If there is s1 in s, replace it with s2, this conversion table corresponds to each character, and does not need to contain all the characters.

Examples

  

1 import string 2 3 s = 'helloworld, 0001111 '4 5 table = string. maketrans ('','') # No ing, keep the original string 6 s. translate (table) # hello world, 0001111 7 s. translate (table, 'hello000) '# world, 1111 8 9 table = string. maketrans ('abcdefgh', 'abcdefgh') 10 s. translate (table) # HEllo, worlD, 000111111 s. translate (table, 'World') # HEllo, 0001111

Now we can talk about makerans and translate packaging to form a factory function that returns the closure (print is the factory function)

 1 import string 2 def translator(frm = '',  to='', delete= '', keep = None): 3     if len(to) == 1: 4         to = to * len(frm) 5     trans = string.maketrans(frm, to) 6     if keep is not None: 7         allchars = string.maketrans('','') 8         delete = allchars.translate(allchars, keep.translate(allchars, delete)) 9         def translate(s):10             return s.translate(trans, delete)11         return translate

Closure is used at the end of the function. Closure refers to the function that has the right to access the variables in another function scope. A common way to create a closure is to create another function within a function:

1 def make_adder(addend):2     def adder(augend): return augend + addend3     return adder

Execute p = make_addr (23) to generate a closure of the inner function addr. This closure references the name addend internally, and addend is bound to the value 23, and execute p (100) then 123 is returned.

Now we have closed all kinds of possibilities behind an interface we suggest using.

>>> Digits_only = translator (keep = string. digits)

>>> Digits_only ('chris Perkins: 224-7992 ')

'123'

Removing elements that belong to a character set is also very simple:

>>> No_digits = translator (delete = string. digits)

>>> No_digits ('chris Perkins: 224-7992 ')

'Chris Perkings :-'

It can also be replaced:

>>> Digits_to_hash = translator (from = string. digits, to = '#')

>>> Digits_to_hash ('chris Perkins: 224-7992 ')

'Chris Perkins :###-####'

When there are overlapping parts of delete and keep, the delete parameter takes priority.

>>> Trans = translator (delete = 'abc', kepp = 'cdef ')

>>> Trans ('abcdefg ')

'Ef'

In fact, you can add some exceptions in more detail to handle the case of delete and keep at the same time.


Usage of python3x translate

>>> A = 'Hello! World! '
>>> T = a. maketrans ('l', 'A ')
>>> A. translate (t)
'Heaao! Worad! '

>>> Ttt = a. maketrans ('lh ',' AB ')
>>> A. translate (ttt)
'Beaao! Worad! '

Translate is a one-to-one ing of characters. Each character will be replaced with the corresponding character whenever it appears.
Replace is a string replacement. After the string is complete, it is replaced as a whole. The length of the two string parameters of replace can be different.

Python Programming Problems

Please refer to: www.jb51.net/article/15701.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.