Python Training Day4 Casual

Source: Internet
Author: User

Lambda expression

This expression, like the three-yard operation, is designed to improve Python's processing speed and simplify code.

But the LAMDA function can only handle simple logic

For example, the following function

def s (x): Return x+1print S (1)
2

If you write with a lambda expression, you can simplify the

S=lambda x:x+1print S (1)
2

Similarly, if there are multiple parameters that can be written

S=lambda X,y:x+yprint S (Sat)
3


Map () function

The function of the map function is to manipulate each element in the sequence and generate a new sequence

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/425762/201511/425762-20151114164016869-1553913346. PNG "alt=" 425762-20151114164016869-1553913346.png "/>

Map () The first parameter in parentheses requires a function, and the second parameter requires a sequence.

Add 100 to each element in the sequence Li, as in the following example

Li=[1,2,3,4,5]s=lambda x:x+100new_list=map (s,li) Print new_list
[101, 102, 103, 104, 105]

If there are two sequences, note that the number of elements in the sequence must be the same, or it will be an error.

Li=[1,2,3,4,5]xi=[10,20,30,40,50]s=lambda x,y:x+ynew_list=map (s,li,xi) Print new_list
[11, 22, 33, 44, 55]


Filter () function

The function of the filter function is to filter the element capacity in the sequence according to certain rules and to generate a new sequence

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/425762/201511/425762-20151114164107884-344713622. PNG "alt=" 425762-20151114164107884-344713622.png "/>

There are two parameters in filter (). The first parameter requires a function, and the second argument is a sequence. The filtering of elements in a sequence is defined by a function.

For example, to remove a number greater than 22 in a sequence

Li=[1,5,11,22,81,100]s=lambda x:x>22new_list=filter (s,li) Print new_list
[81, 100]

or write like this.

Li=[1,5,11,22,81,100]def s (x): if x > 22:return xnew_list=filter (s,li) Print new_list
[81, 100]

If the filter condition is written to none, the original sequence is printed. But the empty values in the sequence are removed

li=[1,5,11,22,81,100, ', ', ' \ n ']def s (x): if x > 22:return xnew_list=filter (none,li) Print new_list
[1, 5, one, $, bayi, +, ', ' \ n ']


Reduce () function

The function of the reduce () function is to accumulate all elements of an element in a cumulative operation

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/425762/201511/425762-20151114164122962-1616737731. PNG "alt=" 425762-20151114164122962-1616737731.png "/>

The reduce () function requires three parameters, the first parameter requires a function, and the second parameter requires a sequence. The third is an optional parameter that represents the cardinality

For example, to accumulate a sequence of operations

Li=[1,2,3,4,5]def s (x, y): Return x+ynew_list=reduce (s,li) Print new_list
15

If you add cardinality, this parameter is equivalent to attaching a cardinality based on the result of the calculation.

Li=[1,2,3,4,5]def s (x, y): Return x+ynew_list=reduce (s,li,1000) Print new_list
1015

If the inside of the first parameter multiplies the elements in the subsequent sequence, then the third cardinality is the equivalent of multiplying the computer by the last

Base

Li=[1,2,3,4,5]def s (x, y): Return x*ynew_list=reduce (s,li,10) Print new_list
1200


Yield generator

When we define a function, the code that follows return will no longer execute if it encounters a return.

Def a (): Return 1 return 2print a ()
1

But if we generate yield, each execution function will record the last position and the next line. However, the returned content must be called for to be seen.

Def a (): Yield 1 yield 2for I in a (): Print I
12

Referring to range (10) and xrange (10), both functions can produce a sequence of 0-9 numbers. Range () works by first creating a number in memory and then printing it. The xrange () that uses the generator () needs to print which number to print to create which number to print, so that it does not consume too much system memory. The generator is widely used when writing database connection pools.


Decorative Device

The adorner is a well-known design pattern, which is often used in scenes where there is a demand for facets, with the classic insert log, performance test, transaction processing, and so on. Decorators are a great design for solving such problems, and with adorners, we can pull out a lot of the same code that is not relevant to the function itself and continue to reuse it. In summary, the function of an adorner is to add additional functionality to an already existing object.

For example, the following function:

Def a (): Return ' 123 ' Print a ()
123

If you want the string ' hello ' to precede the output of the function

Can be implemented by modifying function A (), but if there are many functions like a () that have this functional requirement. It is unrealistic to have each function go to the code to modify it.

By this time, you can use the adorner to achieve

#第一个装饰器函数的名称, the function's argument is the function name Def add_str (func) that needs to add a new function: #再定义一个函数, which puts the content to be added and calls the function to be included Def wrapper (): #这里要添加的str内    Tolerance str= ' Hello ' #这里调用需要被包含的函数, this example is equivalent to S=a () S=func () #对字符串进行拼接 return '%s%s '% (str,s)    #这里一定要加入返回wrapper的内存地址给装饰器, note that it cannot be return wrapper (). Return wrapper# is placed in front of the specified function by the format of the @+ adorner name. Indicates that the function is to be added when the adorner @add_strdef a (): Return ' 123 ' x = A () print X
Hello 123


If the function that was added to the adorner itself would need to pass parameters

For example

def a (m,n): Return ' 123 ' +m+nx = A (' 321 ', ' ABC ') print X
123321ABC

Then you only need to set two parameters in the adorner's inline function, so that the adorner can pass the required arguments of the original function into the adorner interior.

def add_str (func): #再定义一个函数, the function contains the contents to be added and calls to the functions and functions to be included in the necessary Parameters Def wrapper (m,n): str= ' Hello ' #这里调用需要被包含的    Function, this example is equivalent to S=a (m,n) S=func (m,n) return '%s%s '% (str,s) #这里一定要加入返回wrapper的内存地址给装饰器, note that it cannot be return wrapper (). Return wrapper@add_strdef A (m,n): Return ' 123 ' + M + nx = A (' 321 ', ' ABC ') print X
Hello 123321ABC


This article from "Thunderbolt Tofu" blog, declined reprint!

Python Training Day4 Casual

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.