Python Road - Day4

This section outlines

  1. Builder & iterator
  2. Decorator
    1. Basic decorator
    2. Multi-parameter decorator
  3. Recursion
  4. Basic algorithms: binary search, two-dimensional array conversion
  5. Regular Expressions
  6. Common learning module
  7. Job: Development Calculator
    1. Achieve their priority is resolved, Math and extension number
    2. User Input 1 - 2 * ((+ 60-30 (- 40/5) * (9-2 5/3 + 7/3 99/4 2998 + 10 568/14)) - (-4 3) / (16 -3 2 after a similar equation)), etc., must resolve their inside (), +, -, *, / symbols and equations, the outcome of the operation, the result must be true results obtained coincides calculator

Builder & iterator

Iterator iterator
iterator is a way to access the collection elements. Iterator object began a visit from the first element of the collection until all the elements are accessed completed. Iterator can only move forward not backward, but that's OK, because people rarely go back in an iterative way . In addition, a major advantage of the iterator is not required to be prepared in advance throughout the iterative process all the elements. Iterator iterative calculation only when the element to an element, and before or after the element can not exist or be destroyed. This feature makes it particularly suitable for traversing some huge or infinite set, such as a few G's file

Features:

  1. Visitors do not need to be concerned about the internal structure of the iterator, only () methods continue to be removed by a content next
  2. Can not access a random set of values ​​can only be accessed sequentially from beginning to end
  3. During the visit can not retreat back
  4. Cycle facilitates relatively large set of data, to save memory

Generate an iterator:

>>> a = iter([1,2,3,4,5])
>>> a
<list_iterator object at 0x00000000006B3AC8>
>>> a.__next__()
1
>>> a.__next__()
2
>>> a.__next__()
3
>>> a.__next__()
4
>>> a.__next__()
5
>>> a.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Document Summary

Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream. 
When no more data are available a StopIteration exception is raised instead. 
At this point, the iterator object is exhausted and any further calls to its __next__() method just raise StopIteration again.

Generator generator
Definition: Returns an iterator when a function is called, then this function is called Generator (generator). If the function is included yield syntax, then this will become a function generator

def cash_out(amount):
    while amount >0:
        amount -= 1
        yield 1        #print("擦,又来取钱了。。。败家子!")
 
 
 
ATM = cash_out(5)
 
print("取到钱 %s 万" % ATM.__next__())
print("花掉花掉!")
print("取到钱 %s 万" % ATM.__next__())
print("花掉花掉!")
print("取到钱 %s 万" % ATM.__next__())
print("花掉花掉!")
print("取到钱 %s 万" % ATM.__next__())
print("花掉花掉!")
print("取到钱 %s 万" % ATM.__next__())
print("花掉花掉!")
print("取到钱 %s 万" % ATM.__next__()) #到这时钱就取没了,再取就报错了

输出:
取到钱 1 万
花掉花掉!
取到钱 1 万
花掉花掉!
取到钱 1 万
花掉花掉!
取到钱 1 万
花掉花掉!
取到钱 1 万
花掉花掉!
Traceback (most recent call last):
  File "py.py", line 20, in <module>
    print("取到钱 %s 万" % ATM.__next__()) #到这时钱就取没了,再取就报错了
StopIteration

effect:

The yield of the main effects, that is, the function can interrupt and hold interrupt state, after the interruption, the code can continue down, over time, you can then call this function again, one started from the last time the yield.

Further, the concurrent operation may also be implemented by a single thread cases yield in effect

import time
def consumer(name):
    print("%s 准备吃包子啦!" %name)
    while True:
       baozi = yield
 
       print("包子[%s]来了,被[%s]吃了!" %(baozi,name))
 
def producer(name):
    c = consumer('A')
    c2 = consumer('B')
    c.__next__()
    c2.__next__()
    print("老子开始准备做包子啦!")
    for i in range(3):
        time.sleep(1)
        print("做了2个包子!")
        c.send(i)
        c2.send(i)
 
producer("alex")
# 输出
A 准备吃包子啦!
B 准备吃包子啦!
老子开始准备做包子啦!
做了2个包子!
包子[0]来了,被[A]吃了!
包子[0]来了,被[B]吃了!
做了2个包子!
包子[1]来了,被[A]吃了!
包子[1]来了,被[B]吃了!
做了2个包子!
包子[2]来了,被[A]吃了!
包子[2]来了,被[B]吃了!

Decorator

Silver king angle looking directly written document http://www.cnblogs.com/wupeiqi/articles/4980620.html

Recursion

Features
recursive algorithm is a procedure calls itself directly or indirectly algorithm. In computer programming, a recursive algorithm is very effective to solve a large class of problems, it is often the description of the algorithm is simple and easy to understand.
Recursive algorithm to solve the problem of features:

(1) is a recursive procedure or function calls itself inside.

(2) When using recursion policy, it must have a clear recursive end condition, called recursive exports.

(3) recursive algorithm solving often are very simple, but lower recursive algorithm solving operational efficiency. It is generally not advocate using a recursive algorithm design program.

(4) In the process of recursive calls to system return point of each layer, and so opens up a stack local variable storage. Recursive too many times likely to cause a stack overflow. It is generally not advocate using a recursive algorithm design program.

Requirements
recursive algorithm embodied in the "repeat" generally have three requirements:

  1. First, each call on the scale have reduced (usually half);
  2. Second, there is a close link between adjacent repeated twice, once before to prepare for the time after (the normal output time ago as one of the input);
  3. Third, the scale of the problem is extremely small must be given direct answers instead of recursive calls, so that each recursive call is conditional (scale does not meet the conditions for the direct answer of size), unconditional recursive calls will be infinite loop does not end normally.

achieve

  1. Find recursive binary by
      an existing list primes = [2, 3, 5 , 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 , 73, 79, 83, 89, 97], Haier claims 23 to find the fastest way. Please Low B, Low 2B, Low 3B three students to answer this question.
      Low B: This is very simple, direct use if 23 in primes: ( "! Found it") print, voice hardly ever played was the teacher, let yourself realize, do not let you use ready-made function, Low B then said, We can only scratch a number, and then Low B was fired. . .
      Low 2B: Since the list is ordered, I can list half from interception, probably as follows:
        p1 = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 ]
        P2 = [43 is, 47, 53 is, 59, 61 is, 67, 71 is, 73 is, 79, 83, 89, 97]
        and see p1 [-1] is greater than 41 is 23, if a large ratio of 23 represents 23 certainly there p1, p2 otherwise it would certainly inside. Now we know that 23 is smaller than 41, so 23 years certainly p1, p1 but there is still a lot of elements, how to find it 23? Very simple, still according to the previous method, the p1 divided into two parts, as follows:
        P1_a = [2,. 3,. 5,. 7,. 11, 13,17]
        P1_b = [. 19, 23 is, 29, 31 is, 37,41]
        Then we found that 23 to 17 large p1_a last value, that means 23 certainly p1_b in, p1_b there are still many elements, then the method before the press continue and a half, and ultimately take a few times, certainly put 23 to find Out!
        Then, Low 2B full of a sense of accomplishment dumped at the head of dandruff.
      Teacher: Very good, really a lot stronger than the Low B program. Then turned around and asked Low 3B, you have a better idea of it?
    Low 3B: ah. . . Oh, my. . . I like the idea of Low 2B, the result he said.
    Teacher: Oh, you come help me to write the code.
    Low 3B this time a cold sweat, because he did not train of thought, but still bite the bullet and go write. . . Although it did not ideas, but Google will Yeah, three hours later, finally Biechu the following code:
def binary_search(data_list, find_num):
    mid_pos = int(len(data_list) /2 ) #find the middle position of the list
    mid_val = data_list[mid_pos] # get the value by its position
    print(data_list)
    if len(data_list) > 1:
        if mid_val > find_num: # means the find_num is in left hand of mid_val
            print("[%s] should be in left of [%s]" %(find_num,mid_val))
            binary_search(data_list[:mid_pos],find_num)
        elif mid_val < find_num: # means the find_num is in the right hand of mid_val
            print("[%s] should be in right of [%s]" %(find_num,mid_val))
            binary_search(data_list[mid_pos+1:],find_num)
        else: # means the mid_val == find_num
            print("Find ", find_num)
 
    else:
        print("cannot find [%s] in data_list" %find_num)
 
if __name__ == '__main__':
    primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
    binary_search(primes,23)
# 输出
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
[23] should be in left of [41]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
[23] should be in right of [17]
[19, 23, 29, 31, 37]
[23] should be in left of [29]
[19, 23]
Find  23

The story behind the knitting I do not go, ha ha! but anyway, the above is a typical recursive usage, in a program that calls itself.

Algorithms basis

Requirements: generating a two-dimensional array of 4 * 4 and rotating it 90 degrees clockwise

array=[[col for col in range(4)] for row in range(4)] #初始化一个4*4数组
 
for row in array: #旋转前先看看数组长啥样
    print(row)
 
print('-------------')
for i,row in enumerate(array):
 
    for index in range(i,len(row)):
        tmp = array[index][i] #get each row's data by column's index
        array[index][i] = array[i][index] #
        print(tmp,array[i][index])  #
        array[i][index] = tmp
    for r in array: 
        print(r)
 
    print('--one big loop --')
# 输出
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
[0, 1, 2, 3]
-------------
0 0
0 1
0 2
0 3
[0, 0, 0, 0]
[1, 1, 2, 3]
[2, 1, 2, 3]
[3, 1, 2, 3]
--one big loop --
1 1
1 2
1 3
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 3]
[3, 3, 2, 3]
--one big loop --
2 2
2 3
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
--one big loop --
3 3
[0, 0, 0, 0]
[1, 1, 1, 1]
[2, 2, 2, 2]
[3, 3, 3, 3]
--one big loop --

Bubble Sort
an irregular array in ascending sort order

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)
# 输出
before sort: [10, 4, 33, 21, 54, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6]
[4, 10, 21, 33, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 54]
[4, 10, 21, 3, 8, 11, 5, 22, 2, 1, 17, 13, 6, 33, 54]
[4, 10, 3, 8, 11, 5, 21, 2, 1, 17, 13, 6, 22, 33, 54]
[4, 3, 8, 10, 5, 11, 2, 1, 17, 13, 6, 21, 22, 33, 54]
[3, 4, 8, 5, 10, 2, 1, 11, 13, 6, 17, 21, 22, 33, 54]
[3, 4, 5, 8, 2, 1, 10, 11, 6, 13, 17, 21, 22, 33, 54]
[3, 4, 5, 2, 1, 8, 10, 6, 11, 13, 17, 21, 22, 33, 54]
[3, 4, 2, 1, 5, 8, 6, 10, 11, 13, 17, 21, 22, 33, 54]
[3, 2, 1, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[2, 1, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
[1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]
after sort: [1, 2, 3, 4, 5, 6, 8, 10, 11, 13, 17, 21, 22, 33, 54]

The time complexity of
(1) times the frequency of an algorithm to perform time-consuming, in theory can not be counted out, the machine must be running tests to know. But we can not and need for each machine to test the algorithms, just know that it takes more than the time which algorithm, which algorithm takes less time on it. And the time it takes an algorithm is proportional to the number of times the algorithm execution statement. A number of execution algorithms statement called the statement frequency or time frequency. Referred to as T (n).
(2) time complexity at the time just mentioned frequency, n is called the scale of the problem, when changing n, the frequency of the time T (n) will continue to change. But sometimes we want to know what time it renders the law changes. To this end, we introduce the concept of time complexity. In general, the number of basic arithmetic operations are repeatedly performed a function of the problem size n, with T (n) represents, if an auxiliary function F (n), such that when n approaches infinity, T ( n) / f (n) is not equal to zero limit constant, called f (n) is T (n) is a function of the same order. Denoted by T (n) = O (f (n)), said O (f (n)) is a progressive time complexity of the algorithm, referred to as time complexity.

Exponential time
refers to the time calculated m (n) needed to solve a problem, depending on the size of the input data n exponential growth (i.e. the number of input data by a linear growth, the time spent will grow exponentially)

for (i=1; i<=n; i++)
       x++;
for (i=1; i<=n; i++)
     for (j=1; j<=n; j++)
          x++;

The time complexity for a first cycle was Ο (n), for the second cycle of the time complexity is Ο (n ^ 2), the time complexity of the algorithm is Ο (n + n ^ 2) = Ο (n ^ 2).

Time constant
if an algorithm for, T (n) is independent of the size of the upper bound of the input, which is said to have a time constant, referred to as O (1) time. One example is a single element of the array can be accessed, because it requires an access instruction. However, finding the smallest element in the array of disorder is not, because it needs to traverse all the elements to find the minimum. It is a linear-time operation, also known as O (n) time. However, if the number of elements known in advance and the number of assumptions remain constant, the operation may also be referred to as having a constant time.

Logarithmic time

If the algorithm is T (n) = O (log n), which is said to have a logarithmic time

Common binary tree algorithm has had a number of time-related operations and binary search.

Logarithmic time algorithm is very effective, because each additional input, they need extra computing time will be smaller.

Recursively cut half and the output string is a simple example. It takes O (log n) time because before each output string we will cut in half. This means that if we want to increase the number of output, you need the string length is doubled.

Linear time 
time complexity if an algorithm is O (n), is said to have a linear time algorithm, or O (n) time. Informally, this means that the input of sufficient scale, the scale of how much input the running time is linear. For example, the calculation program and a list of all the elements, and the time required is proportional to the length of the list.

Regular Expressions

import re 
p = re.compile("^[0-9]")  #生成要匹配的正则对象 , ^代表从开头匹配,[0-9]代表匹配0至9的任意一个数字, 所以这里的意思是对传进来的字符串进行匹配,如果这个字符串的开头第一个字符是数字,就代表匹配上了
m = p.match('14534Abc')   #按上面生成的正则对象 去匹配 字符串, 如果能匹配成功,这个m就会有值, 否则m为
if m:
    print(m.group())          #m.group()返回匹配上的结果,此处为1,因为匹配上的是1这个字符
else:
    print("It doesn't match.")

The above lines 2 and 3 may be combined into a line to write:

m = re.match("^[0-9]",'14534Abc')

The effect is the same, the difference is that the first way is to match the format of advance has been compiled (to match the format parsing), when such a match would not go again compiled format that matches the first two kinds of shorthand per times when the match should compile a match format, so if you need to match a file from 50,000 lines in the all begins with a number of lines, it is recommended to first compile the regular expression match again, this will be faster point.

mode description
^ Matches the beginning of the string
$ Matches the end of the string.
. Matches any character except newline, when re.DOTALL flag is specified, will match any character comprises a newline.
[...] It used to represent a group of characters, listed separately: [amk] matches 'a', 'm' or 'k'
[^...] Not in [] characters: [^ abc] In addition to matching a, b, c character.
re* Zero or more matches of expression.
re+ Match one or more of the expressions.
re? Match 0 or 1 by the preceding regular expression defined fragments, non-greedy manner
re{n}
re{n,} Exactly matching the n preceding expression.
re{n, m} N m times to match the regular expression by the preceding definition segment, greedy manner
a|b Matches a or b
(re) Matching expression in parentheses, also represents a group
(?imx) Optional regular expression comprises three flags: i, m, or x. It affects only the area parentheses.
(?-imx) Regex off i, m, x or optional flag. It affects only the area parentheses.
(?: re) Similar (...), but does not represent a group
(?imx: re) Use in parentheses i, m, x or optional flag
(?-imx: re) Not used i, m in brackets, or alternatively mark x
(?#...) Note
(?= re) Before the delimiter to be sure. If the regular expression contained in the order ... said the success of the success of the current match position, otherwise fail. But once contained expressions have been tried, the matching engine does not advance; the remainder of the pattern is even attempt the right delimiter.
(?! re) Delimiter before the negative. In contrast with the positive assertion; successful when expression contained in the string does not match the current position
(?> re) Independent pattern matching, eliminating backtracking.
\w Match an Alphanumeric
\W Matching non-alphanumeric
\s Matches any whitespace character, equivalent to [\ t \ n \ r \ f].
\S Matches any non-blank character
\d Match any number, is equivalent to [0-9].
\D Matches any non-digit
\A Matching string start
\FROM Matching string end, if it is the wrap, before the end of the string match only to wrap. c
\from The end of the match string
\G The last match to complete the match position.
\b Matches a word boundary, that is, refers to the location and spaces between words. For example, 'er \ b' matches "never" in the 'er', but does not match the "verb" in the 'er'.
\B Matching non-word boundary. 'Er \ B' matches "verb" in the 'er', but does not match the "never" in the 'er'.
\ N, \ t, etc. Matches a newline. A matching tab. Wait
\1...\9 Subexpression matches the n-th packet.
\10 Subexpression matches the n-th packet, if it is matched. Otherwise, the expression refers to the octal character code.

正则表达式常用5种操作
re.match(pattern, string)     # 从头匹配
re.search(pattern, string)    # 匹配整个字符串,直到找到一个匹配
re.split()            # 将匹配到的格式当做分割点把字符串分割成列表

import re
m = re.split("[0-9]", "alex1rain2jack3helen rachel8")
print(m)
#output
['alex', 'rain', 'jack', 'helen rachel', '']

re.findall()          # 找到所有要匹配的字符并返回列表格式

>>> m = re.findall("[0-9]", "alex1rain2jack3helen rachel8")
>>> print(m)
['1', '2', '3', '8']

re.sub(pattern, repl, string, count, flag)    # 替换匹配到的字符

>>> m=re.sub("[0-9]","|", "alex1rain2jack3helen rachel8",count=2)
>>> print(m)
alex|rain|jack3helen rachel8

正则表达式实例
字符匹配

实例 |描述
python |匹配 "python".

字符类

实例 |描述
[Pp]ython |匹配 "Python" 或 "python"
rub[ye] |匹配 "ruby" 或 "rube"
[aeiou] |匹配中括号内的任意一个字母
[0-9] |匹配任何数字。类似于 [0123456789]
[a-z] |匹配任何小写字母
[A-Z] |匹配任何大写字母
[a-zA-Z0-9] |匹配任何字母及数字
[^aeiou] |除了aeiou字母以外的所有字符
[^0-9] |匹配除了数字外的字符

特殊字符类

实例 |描述
. |匹配除 "\n" 之外的任何单个字符。要匹配包括 '\n' 在内的任何字符,请使用象 '[.\n]' 的模式。
\d |匹配一个数字字符。等价于 [0-9]。
\D |匹配一个非数字字符。等价于 [^0-9]。
\s |匹配任何空白字符,包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
\S |匹配任何非空白字符。等价于 [^ \f\n\r\t\v]。
\w |匹配包括下划线的任何单词字符。等价于'[A-Za-z0-9_]'。
\W |匹配任何非单词字符。等价于 '[^A-Za-z0-9_]'。

re.match与re.search的区别
re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;而re.search匹配整个字符串,直到找到一个匹配。

Regular Expression Modifiers: Option Flags
Regular expression literals may include an optional modifier to control various aspects of matching. The modifiers are specified as an optional flag.
You can provide multiple modifiers using exclusive OR (|), as shown previously and may be represented by one of these

Modifier |Description
re.I |Performs case-insensitive matching.
re.L |Interprets words according to the current locale. This interpretation affects the alphabetic group (\w and \W), as well as word boundary behavior (\b and \B).
re.M |Makes $ match the end of a line (not just the end of the string) and makes ^ match the start of any line (not just the start of the string).
re.S |Makes a period (dot) match any character, including a newline.
re.U |Interprets letters according to the Unicode character set. This flag affects the behavior of \w, \W, \b, \B.
re.X |Permits "cuter" regular expression syntax. It ignores whitespace (except inside a set [] or when escaped by a backslash) and treats unescaped # as a comment marker.

几个常见正则例子
匹配手机号

import re
phone_str = "hey my name is alex, and my phone number is 13651054607, please call me if you are pretty!"
phone_str2 = "hey my name is alex, and my phone number is 18651054604, please call me if you are pretty!"
 
m = re.search("(1)([358]\d{9})",phone_str2)
if m:
    print(m.group())

匹配IP V4

import re
ip_addr = "inet 192.168.60.223 netmask 0xffffff00 broadcast 192.168.60.255"
m = re.search("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}", ip_addr)
print(m.group())

分组匹配地址 

>>> contactInfo = 'Oldboy_School, Beijing_Changping_Shahe: 010-8343245'
>>> match = re.search(r'(\w+), (\w+): (\S+)', contactInfo) #分组
>>> match
<_sre.SRE_Match object; span=(0, 51), match='Oldboy_School, Beijing_Changping_Shahe: 010-83432>
>>> match.group(1)
'Oldboy_School'
>>> match.group(2)
'Beijing_Changping_Shahe'
>>> match.group(3)
'010-8343245'

>>> match = re.search(r'(?P<last>\w+), (?P<first>\w+): (?P<phone>\S+)', contactInfo)
>>> match
<_sre.SRE_Match object; span=(0, 51), match='Oldboy_School, Beijing_Changping_Shahe: 010-83432>
>>> match.group(1)
'Oldboy_School'
>>> match.group(2)
'Beijing_Changping_Shahe'
>>> match.group(3)
'010-8343245'

匹配email

import re
email = "[email protected]   http://www.oldboyedu.com"
m = re.search(r"[0-9.a-z]{0,26}@[0-9.a-z]{0,20}.[0-9a-z]{0,8}", email)
print(m.group())
#output
[email protected]

json 和 pickle

用于序列化的两个模块

  • json,用于字符串 和 python数据类型间进行转换
  • pickle,用于python特有的类型 和 python的数据类型间进行转换
  • json模块提供了四个功能:dumps、dump、loads、load
  • pickle模块提供了四个功能:dumps、dump、loads、load

其它常用模块学习
http://www.cnblogs.com/wupeiqi/articles/4963027.html

Guess you like

Origin www.cnblogs.com/chenjo/p/12340092.html