Python Learning Note 5-closing and builder

Source: Internet
Author: User

>>> Import re>>> re.search (' [ABC] ', ' Mark ') <_sre. Sre_match object; Span= (1, 2), match= ' a ' >>>> re.sub (' [ABC] ', ' o ', ' Mark ') ' Mork ' >>> re.sub (' [ABC] ', ' o ', ' carp ') ' Oorp '

Note:

[ABC]--match any one of the characters in the A,b,c

[^ABC]--match any character except A,b,c

>>> Import re>>> re.search (' [^aeiou]y$ ', ' vancancy ') <_sre. Sre_match object; Span= (6, 8), match= ' Cy ' >>>> re.sub (' y$ ', ' ies ', ' vacancy ') ' Vacancies ' >>> re.sub (' ([^aeiou]) y$ ', R ' \1ies ', ' vacancy ') ' vacancies '

Note:

\1--means that the first matching grouping is placed in that location. If you have more than one grouping, you can use \2, \3, and so on.

List of functions:

A. The technique of using external parameter values in dynamic functions is called closure.

Import Redef build_match_and_apply_functions (pattern,search,replace):d ef matches_rule (Word): Return Re.search ( Pattern,word) def apply_rule (word): return re.sub (Search,replace,word) return (Matches_rule,apply_rule)

B.

>>> patterns = (' [sxz]$ ', ' $ ', ' es '), (' [aeioudgkprt]h$ ', ' $ ', ' es '), (' (qu|[ ^aeiou]) y$ ', ' y$ ', ' ies '), (' $ ', ' $ ', ' s ')) >>> rules = [Build_match_and_apply_functions (pattern, search, Replace) for (pattern, search, replace) in Patterns]>>> rules[(<function build_match_and_apply_functions. <locals>.matches_rule at 0x10384d510>, <function build_match_and_apply_functions.<locals>.apply_ Rule at 0x10384d598>), (<function build_match_and_apply_functions.<locals>.matches_rule at 0x10384d620 <function Build_match_and_apply_functions.<locals>.apply_rule at 0x10384d6a8>), (<function Build_match_and_apply_functions.<locals>.matches_rule at 0x10384d730>, <function build_match_and_apply _functions.<locals>.apply_rule at 0x10384d7b8>), (<function build_match_and_apply_functions.<locals >.matches_rule at 0x10384d840>, <function build_match_and_apply_functions.<locals>.apply_rule at 0x10384d8c8>)]>>>  

C. Matching pattern file

Import Redef build_match_and_apply_functions (pattern, search,replace):d EF matches_rule (Word): Return Re.search ( Pattern,word) def apply_rule (word): return re.sub (Search,replace,word) return (matches_rule,apply_rule) rules = []with Open (' plural4-rules.txt ', encoding = ' utf-8 ') as Pattern_file:for line in pattern_file:pattern,search,replace = Line.split (none,3) rules.append (Build_match_and_apply_functions (pattern,search,replace))

1) Open (): Opens the file and returns a file object.

2) ' with ': Creates a context: when the With statement ends, Python automatically closes the file, that is, the open is an unexpected occurrence.

3) Split (none,3): None indicates that any whitespace characters (spaces, tables, and so on) are delimited. 3 indicates that the remaining portion of the line is discarded three times for whitespace.

Generator:

>>> def make_counter (x):p rint (' entering Make_counter ') while True:yield xprint (' incrementing x ') x=x+1> >> counter = make_counter (2) >>> Counter<generator object Make_counter at 0x1038643f0>>>> Next (counter) entering make_counter2>>> next (counter) incrementing x3>>> next (counter) incrementing X4>>> Next (counter) incrementing X5

def fib (max): b = 0, 1while a < Max:yield AA, B = b, a+b>>> for n in fib ():p rint (n,end= ") 0 1 1 2 3 5 8 >>> List (FIB (200)) [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

1) Pass a generator to the list () function, which will traverse the entire generator and return all generated values.

Python Learning Note 5-closing and builder

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.