The application of closures in Python translate and Maketrans usage _python

Source: Internet
Author: User

Python's handling of strings is relatively efficient and there are many ways to do it. Among them, Maketrans and translate two methods have been applied a lot, this article will do a summary of the use of these two methods.

Let's start with a review of the two methods:

①s.translate (TABLE,STR) removes the characters that str contains from the string s, and the remaining strings are replaced by the character mapping relationships in the table. Table can be understood as a conversion table, comparing ' a '-> ' a ', ' B '-> ' B '.

②tabel = String.maketrans (' s1 ', ' s2 ') S1 and S2 must be the same length, Maketrans generate a conversion table, if there is S1 in S, then replace with S2, the conversion table is the character of a corresponding, not necessarily all inclusive.

Give a few examples:

Import string
s = ' HelloWorld, 0001111 '
table = String.maketrans (', ') #没有映射, keep the original string
s.translate (table) # Hello World, 0001111
s.translate (table, ' hello000) ' #world, 1111
table = String.maketrans (' abcdefgh ', ' Abcdefgh ')
s.translate (table) #HEllo, world,0001111
s.translate (table, ' World ') #HEllo, 0001111

We can now wrap the makerans,translate to form a factory function that returns a closure (print is a factory function), as follows:

Import String
def translator (frm = ', to= ', delete= ', keep = None):
  If Len (to) = = 1: to
    = to * len (frm) 
   trans = String.maketrans (frm, to)
  if keep are not None:
    allchars = String.maketrans (",")
    delete = Allchars . Translate (Allchars, keep.translate (allchars, delete))
    def translate (s): Return
      s.translate (trans, delete) return
    Translate

The closure is used at the end of the function, which refers to a function that has access to variables in the scope of another function. A common way to create closures is to create another function inside one function:

def make_adder (addend):
   def adder (augend): return augend + addend return
   adder

The execution P = make_addr (23) produces a closure of the inner function addr, which references the name Addend internally, and the Addend is bound to the value 23, and the execution p (100) eventually returns 123.

Now we have closed the possibilities behind a suggested interface.

>>>digits_only = Translator (keep = string.digits)
>>>digits_only (' Chris perkins:224-7992 ')
' 2247992 '

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:-'

You can also replace:

>>>digits_to_hash = Translator (from = string.digits, to = ' # ')
>>>digits_to_hash (' Chris Perkins : 224-7992 ')
' Chris Perkins: ###-#### '

When delete and keep have overlapping portions, the delete parameter takes precedence

>>>trans = Translator (delete = ' ABCD ', Kepp = ' cdef ')
>>>trans (' ABCDEFG ')
' EF '

In fact, you can add some more details to deal with the occurrence of delete,keep at the same time.

I hope this article will help you with your Python programming.

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.