Python Basics (four) regular, recursive generators

Source: Internet
Author: User
Tags generator generator instance method locale python decorator

Strings are the most data structure involved in programming, and the need to manipulate strings is almost ubiquitous. For example, to determine whether a string is a legitimate email address, although it can be programmed to extract the substring before and after, and then judge whether it is a word and domain name, but this is not only cumbersome, and the code is difficult to reuse.

A regular expression is a powerful weapon used to match strings.    Its design idea is to use a descriptive language to define a rule for a string, and any string that conforms to the rule, we think it "matches", otherwise the string is illegal. The following diagram shows the flow 1 using regular expression matching, the regular expression metacharacters supported by Python, and the syntax
Grammar Description An instance of an expression Full-Match string
Character
General characters Match yourself Abc Abc
. Match any character "\ n" except
Dotall mode (Re.dotall) can also match line breaks
A.b ABC or ABC or A1C, etc.
[...] The character set [ABC] represents a or B or C, or it can-represent a range such as [a-d] for a or B or C or D A[bc]c ABC or ADC
[^...] Non-character sets, which are characters other than [] A[^bc]c ADC or AEC, etc.
Predefined character sets (can also be tied to character sets [...] In
\d Number: [0-9] A\dc A1C, etc.
\d Non-numeric: [^0-9] or [^\d] A\dc ABC, etc.
\s White space character:[< space >\t\n\f\v] A\sc A B etc
\s Non-whitespace characters: [^s] A\sc ABC, etc.
\w Alpha-Numeric (word character) [a-za-z0-9] A\wc ABC or A1C, etc.
\w Non-alphanumeric (non-word character) [^\w] A\wc A.C or A_c, etc.
Quantity words (used in characters or (...) After grouping)
* Match 0 or more of the preceding expressions. (note including 0 times) abc* AB or ABCC, etc.
+ Match 1 or more of the preceding expressions. abc+ ABC or ABCC, etc.
? Match 0 or 1 preceding expressions. (note including 0 times) Abc? AB or ABC
{m} Match m front expression (non-greedy) ABC{2} Abcc
{m,} Match at least m front expression (m to infinity) Abc{2,} ABCC or ABCCC, etc.
{M,n} Match m to n previous expressions abc{1,2} ABC or ABCC
Boundary match (does not consume characters in the character to be matched)
^ Matches the beginning of the string, matching the beginning of each line in multiline mode ^abc ABC or ABCD, etc.
$ Matches the end of the string, matching the end of each line in multiline mode abc$ ABC or 123ABC etc.
\a Match string start only \aabc ABC or ABCD, etc.
\z Match string End only Abc\z ABC or 123ABC etc.
\b Matches a word boundary, which is the position between a word and a space. For example, ' er\b ' can match ' er ' in ' never ', but not ' er ' in ' verb '.
\b Matches a non-word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.
Logic, grouping
| or left or right expression any one (short-circuit) if | The entire regular expression is not represented in () (note the difference between parentheses and no parentheses) Abc|def
AB (c|d) EF
ABC or DEF
ABCEF or Abdef
(...) Groupings, which can be used to refer to, or in parentheses, as a set of numbers matched followed by quantity words (ABC) {2}a Abcabca
(? P<name>, ...) Group alias, name the group, convenient to call later
\<number> A string that refers to a grouping of <number> that is matched to (note that the string is not the grouping expression itself) (\d) abc\1 1AB1 or 5AB5, etc.
(? =name) A string that references the grouping of aliases to name (note that the string being matched is not the grouping expression itself) (? p<id>\d) ABC (? P=id) 1AB1 or 5AB5, etc.
2. Greedy mode and sub-greedy pattern regular expressions are commonly used to find matching strings in text. The number of words in Python is greedy by default (which may be the default non-greedy in a few languages), always trying to match as many characters as possible, and not greedy, instead, always trying to match as few characters as possible. For example: the regular expression "ab*" will find "abbb" if it is used to find "ABBBC". And if you use a non-greedy quantity word "ab*?", you will find "a". 3, Python's Re module Python provides support for regular expressions through the re-module. The general step for using re is to compile the string form of the regular expression into a pattern instance, then use the pattern instance to process the text and get the matching result (a match instance), and finally use the match instance to get the information and do other things.
1 Import re 2   3 # Compile the regular expression into a pattern object 4 pattern = re.compile (R ' Hello ') 5   6 # Use pattern to match the text, get the match result, cannot match when will return none 7 Ma tch = Pattern.match (' Hello world! ')  8   9 If match:10     # use match to get Group Info     print Match.group ()
Execution results
Hello
Of course, you can directly use the method of the re module to pass the regular expression parameters directly to complete
1 macth = re.match (' Hello ', ' Hello World ') 2 if Match:3     print Match.group ()
The execution results are the same. Common methods for  4, re modules re.compile (strpattern[, flag])      parameters:         strpattern: Regular Expressions         flag: Match mode, selectable values are              re. I (re. IGNORECASE): Ignore case (full notation within parentheses)             m (MULTILINE): Multiline mode, change ' ^ ' and ' $ ' ' Behavior (see)             s (dotall): Point any matching pattern, change '. ' Behavior             l (locale): Make a predetermined character class \w \w \b \b \s \s depending on the current locale   &NBSP;&NBS p;        u (Unicode): Make a predetermined character class \w \w \b \b \s \s \d \d Depending on the character attributes of the UNICODE definition   &NBSP;&NBSP;&NB Sp       x (VERBOSE): Verbose mode. In this mode, the regular expression can be multiple lines, ignore whitespace characters, and can be added to comments.     Return value: The Pattern object is a compiled regular expression that can be matched by a series of methods provided by pattern to find      The following method can be either an instance method of the Pattern object or a method of the re module, with a slightly different syntax match (string[, pos[, Endpos]) | Re.match (pattern, string[, flags])      This method will be from SThe Tring Pos tries to match the pattern at the subscript, and returns a match object if the pattern is still matched at the end, or none if pattern does not match during the match, or if the match does not end to reach Endpos. The default values for     pos and Endpos are 0 and Len (string), Re.match () cannot specify both parameters, and the parameter flags specify a matching pattern when compiling pattern.      Note: This method is not an exact match. If the string has any remaining characters at the end of the pattern, it is still considered successful. If you want an exact match, you can add the boundary match ' $ ' at the end of the expression.       Parameters:    string: string to match     pos: Match start subscript      Endpos: Matching end subscript     pattern: Regular expression     flags: Match mode     return value: If the match is returned successfully, the Nonesearch is returned ( string[, pos[, Endpos]) | Re.search (pattern, string[, flags])      This method is used to find substrings in a string that can match a success. Attempts to match the pattern from the POS subscript of string, returns a match object if the pattern ends with a match, and tries to match the POS after 1 if it does not match, and returns none until Pos=endpos is still not matched. The default values for POS and Endpos are 0 and Len (string), and Re.search () cannot specify both parameters, and the parameter flags specify a matching pattern when compiling pattern.      Parameters: Same match    return value: Same as match     Let's look at the difference between the two methods in one example
>>> Import re>>> s = ' Hello World ' >>> print (Re.match (' Ello ', s)) none>>> print ( Re.search (' Ello ', s)) <_sre. Sre_match object; Span= (1, 5), match= ' ello ' >    Description: You can see the MACTH only match the beginning, does not match the beginning, does not count matching to, search can be from the middle, as long as there can match to even if matching findall (string[, pos[, Endpos]]) | Re.findall (pattern, string[, flags])    searches for a string, returning all matching substrings in a list. A bit like the extension of search, put all matching substrings into a list    parameter: Same match    return value: All matched substrings, no matches return empty list >>> import re             >>> s = ' One1two2three3four4 ' >>> re.findall (' \d+ ', s) [' 1 ', ' 2 ', ' 3 ', ' 4 ']
Split (string[, Maxsplit]) | Re.split (Pattern, string[, Maxsplit]): Splits the string according to the matching substring, returning the split list parameter: string: The string to be split pattern: regular Expression Maxsplit : Maximum number of splits return value: Split List instance
>>> Import re                 >>> s = ' one1two2three3four4 ' >>> re.split (' \d+ ', s) [' One ', ' I ', ' three ', ' Four ', ']
Sub (repl, string[, Count]) | Re.sub (Pattern, REPL, string[, Count]) uses REPL to replace each substring parameter that matches in string: Repl: Replacement string or method, here's how to say this method, the method receives the Macth object, the return value of the method as    The replaced string, in other words, the processed string. String: Pattern of strings to be replaced: regular expression count: Number of instances of substitution: For Repl is a method of the case, just this job to use, to replace a number of it is difficult to cross the case. Suppose we have a arithmetic expression '--(1.1+1+1-( -1)-(1+1+ (1+1+2.2))) +-----111+--++--3-+++++++---+---1+4+4/2+ (1+3) *4.1+ (2-1.1) *2/2*3 ', followed by an odd number of minus signs equal to the principle of being otherwise negative, we can
1 if __name__ = = ' __main__ ': 2     import re 3     s = '--(1.1+1+1-( -1)-(1+1+ (1+1+2.2))) +-----111+--++--3-+++++++---+-- -1+4+4/2+ (1+3) *4.1+ (2-1.1) *2/2*3 ' 4     def replace_sign (expression): 5         "6         replaces multiple consecutive +-symbols, such as +-----, Substituting the odd number of minus signs equals positive otherwise negative principle to replace 7         :p Aram expression: expression, including parenthesized case 8         : return: Returns the processed expression 9         ""         def re_sign (m) :             if M:12                 if M.group (). Count ('-')%2 = = 1:13                     return '-'                 else:15                     return ' + '             else:17                 return '         expression = re.sub (' [\+\-]{2,} ', re_sign, expression)         return Expression20  21     s = replace_sign (s)     print (s)

1. Iterators & Generators

2. Decorative Device

1. Basic Decorator

2. Multi-parameter adorner (learn)

3. Recursion

4. Algorithm Basics: Binary lookup, two-dimensional array conversion, bubble sort

5. Regular expressions

Iterators & Generators

Iterators

Iterators are a way to access the elements of a collection. An iterator object is accessed from the first element of the collection, knowing that all of the elements are accessed at the end. Iterators can only move forward and not back, and one of the big advantages of iterators is that they do not require that all elements in the entire iteration be ready for implementation. An iterator evaluates an element only when it iterates over it. This feature makes iterators particularly useful for facilitating large or infinite combinations, such as a few G log files.

Characteristics:

1. The visitor does not need to be concerned about the structure within an iterator, it is only necessary to continue to fetch the next content through the next () method

2. A value in the collection cannot be accessed randomly, and can only be accessed from beginning to end

3. Cannot be rolled back during access

4. Easy to recycle large data collection, save memory.

Generate an iterator as follows:

View Code

Note: iterators cannot arbitrarily access a value in a collection, and can only be accessed from start to finish, and cannot be rolled back when accessed

Generator generator:

Definition: When a function call returns an iterator, the function is called the Generator (generator), and if the function contains the yield syntax, the function becomes the generator.

The code is as follows:

View Code

Effect: The main effect of this yield is to interrupt the function running state, and keep the interrupt state, after the interruption, the code can continue to execute, after a period of time, you can also recall this function, from the last yield of the next sentence to start execution.

In addition, it is possible to achieve the effect of concurrent operations in single-threaded situations with yield implementation (asynchronous):

The code is as follows:

123456789101112131415161718 importtimedefconsumer(name):    print(‘%s 要吃包子了‘%name)    whileTrue:        baozi =yield        print(‘%s 包子来了,包子要被%s 吃掉了‘%(baozi,name))defproducer(name):    =consumer(‘A‘)    C1 =consumer(‘B‘)    C.__next__()    C1.__next__()    print(‘我要开始做包子啦‘)    forinrange(10):        time.sleep(1)        print(‘%s 做了两个包子‘%name)        C.send(i)        C1.send(i)producer(‘test‘)

Adorner:

Python decorator see another piece of documentation.

Recursive

Characteristics

Recursive algorithm is a direct or indirect process of invoking its own method, it often makes the description of the algorithm and easy to understand.

The recursive algorithm solves the problem with the following characteristics:

(1) Recursion is the invocation of itself in a procedure or function.

(2) When using a recursive strategy, there must be a definite recursive end condition called the recursive exit

(3) Recursive algorithm is usually very indirect, but the performance of recursive algorithm is very low, so it is generally not advocated by recursive algorithm design program.

In the process of recursive invocation, the system opens up stacks for each layer's return points, local variables, etc. Recursive process is prone to stack overflow and so on, so it is generally advocated by recursive algorithm design program.

Requirements

The recursive algorithm embodies the "repetition" generally has three requirements:

One is that each call shrinks in size (usually halved)

Second, there is a close relationship between two repetitions, the previous time to prepare for the next time (usually the previous output is for the next time the input to pave)

Third, in the scale of the problem and no longer recursive calls, so each recursive call is conditional, unconditional exit recursive call will eventually fall into a dead loop.

The sample code is as follows:

Finding a data in a binary way with a large amount of data

def searchdate (data_result,find_n):    mid = Int (len (data_result)/2)    If Len (data_result) >1:        if Find_n < Data_result[mid]:            print (' Find_n in [mid] left ')            searchdate (data_result[:mid],find_n)        elif find_n > Data_result[mid]:            print (' Find_n in [mid] right ')            searchdate (data_result[mid:],find_n)        else:            Print (' Find date in Data_result [%s] '%data_result[mid]) if __name__ = = ' __main__ ':    dabc = List (range (1,99999))    searchdate (dabc,57668)

By using the binary algorithm, we can quickly implement the data search, especially in the case of large data volume, the effect will be better.

Algorithm Basics

1. Generate a two-dimensional array of 4*4 and rotate it instantaneously 90 degrees

# Generate a two-dimensional array
date = [[AAA for AAA in Range (4)] for BBB in range (4)]
Name = ' print '
result = Name.center (30, ' * ')
For I in Date:
Print (i)
For R_index, row in enumerate (date): # Loops through the array and gets the array subscript of the first layer
For C_index in range (R_index, len): # Gets the second layer based on the subscript of the first layer
TMP = Date[c_index][r_index] # The element to be swapped is first stored in a temporary variable
Date[c_index][r_index] = Row[c_index] # Element Exchange
Date[r_index][c_index] = tmp
Print (Result)

for R in Date:
Print (R)
################# #另外一种写法 ##################
data = [[AAA for AAA in Range (4)] for BBB in range (4)]
For I in range (data.__len__ ()):
for k in range (data.__len__ ()):
if (i>k):
temp = Data[i][k]
data[i][k]= Data[k][i]
Data[k][i]=temp

For I in data:
Print (i)

PS: For the first kind of writing is still not very understanding, late need to watch the video, deepen

2. Fibonacci Sequence algorithm:

In a nutshell, the Fibonacci sequence is the third value equals the first value + the sum of the second value, as follows:

1 2 3 5 8 13 21 34 .....

The sample code is as follows:

def calr (arg1,arg2,stop):    if arg1 = = 0:        print (arg1,arg2)    arg3 = arg1 + arg2    print (ARG3)    if Arg3 < Stop:        calr (arg2,arg3,stop) calr (0,1,789)

3. Bubble sort

Sort an irregular array in order from small to large

The code is as follows:

data = [10,4,33,21,54,3,8,11,5,22,2,1,17,13,6] Print ("Before sort:", data) Previous = Data[0]for J in range (len (data)): TMP = 0 for i in range (len (data)-1): if data[i] > data[i+1]: tmp=data[i] data[i] = data [I+1] data[i+1] = tmp print (data) print ("After sort:", data)


############### #另外一种写法 ###################
data = [11, 1231, 124, 24123, 554, 632, 591, 112, 109, 889, 100]
Print (' Before sort: ', data)
For I in range (len (data)):
for k in range (i):
If Data[i] < Data[k + 1]:
Data[i], data[k + 1] = data[k + 1], Data[i]
Print (' After sort: ', data)

Python Basics (four) regular, recursive generator

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.