【Class 18】python 匿名函数 lambda表达式

lambda表达式

lambda 参数一,参数二:表达式
实例一:

def add(x, y):
    return x+y
print(add(1, 2))

#匿名函数
f = lambda x,y: x+y
print( f(1,2) )

输出结果:
3
3

python 三元表达式

x if x > y else y

# x > y ? x : y
x = 1
y = 3
r = x if x > y else y
print(r)

map(fucn, list) 将list中每个元素都传入func,将结果以list返回

实例一:

# 求一个数组内每个数字的平方
list_x = [1,2,3,4,5,6,7]

def square(x):
    return x * x

# for x in list_x:
#    square(x)

r = map(square, list_x)
print( list(r) )

实例二:

# 求一个数组内每个数字的平方
list_x = [1,2,3,4,5,6,7]
r = map(lambda x:x*x, list_x)
print(list(r))

实例三:

# 求一个数组内每个数字的平方
list_x = [1,2,3,4,5,6,7]
list_y = [1,2,3,4,5,6,7]
r = map(lambda x , y : x * x + y, list_x,list_y)
print(list(r))
输出结果:
[2, 6, 12, 20, 30, 42, 56]

## map 的输出结果,取决于 后面集合中较少的那一个
# 求一个数组内每个数字的平方
list_x = [1,2,3,4]
list_y = [1,2,3,4,5,6,7]
r = map(lambda x , y : x * x + y, list_x,list_y)
print(list(r))
输出结果:
[2, 6, 12, 20]

reduce(func, list) 连续计算

每次运行结果,都会作为下一次运行的参数,参与计算
reduce( lambda x,y:x+y, list_x , 初始值)

from functools import reduce

list_x = [1,2,3,4,5,6,7]

# 连续计算,连续调用 lambda
# 初始时: 先从列表取 1,2 ,赋值x=1,y=2,  结果 3
# 第二次: 将上次运行结果3 赋值x, 从列表取3 赋值y, 结果 6
# 第三次: 将上次运行结果6 赋值x, 从列表取4 赋值y, 结果 10
# 第四次: 将上次运行结果10 赋值x, 从列表取5 赋值y, 结果 15
# 第五次: 将上次运行结果15 赋值x, 从列表取6 赋值y, 结果 21
# 第六次: 将上次运行结果21 赋值x, 从列表取7 赋值y, 结果 28
r = reduce( lambda x,y:x+y, list_x )
print( r )

输出结果:
28

filter(func, list) 通过判断 func返回的真假来判断 x 是否要保留在某集合中

list_x = [1,2,3,4,5,6,7]

# 将list 中所有偶数 都剔除
# 如果 x % 2 是真,则返回True, 将x保留在集合中, 如果返回False,则从集合中舍弃x
r = filter( lambda x : True if x % 2 else False, list_x )
print( list(r) )
输出结果:
[1, 3, 5, 7]

装饰器: 注解

#装饰器结构
def decorator(func):
def wrapper():
print(time.time())
func()
return wrapper

当有函数需要添加装饰器的时候,使用 @decorator 即可
@decorator
def f3():

#装饰器

import time

def f1():
    print(time.time())
    print('This is f1 function')

def f2():
    print('This is f2 function')

# 如果有很多类似f1,f2的函数都需要新增一个打印时间,不可能所有函数都去改
def print_current_time(func):
    print(time.time())
    func()

print_current_time(f1)
print_current_time(f2)

打印结果:
1550904894.0954685
1550904894.0954685
This is f1 function
1550904894.09644
This is f2 function


=================================================
改进版本,使用装饰器来实现上述功能
import time

def f2():
    print('This is f2 function')

# 装饰器结构
def decorator(func):
    def wrapper():
        print(time.time())
        func()
    return wrapper

f = decorator(f2)
f()

print("-----装饰器的好处-----")

@decorator
def f3():
    print('This is f3 function')

f3()
输出结果:
1550905474.7415645
This is f2 function
-----装饰器的好处-----
1550905474.7415645
This is f3 function

实例二:

import time

# 装饰器结构
def decorator(func):
    def wrapper(*args):
        print(time.time())
        func()
    return wrapper

def f2():
    print('This is f2 function')

@decorator
def f3():
    print('This is f3 function')
    
f2()
print("-----装饰器的好处-----")
f3()

输出结果:
This is f2 function
-----装饰器的好处-----
1550905938.4208028
This is f3 function

带参数的装饰器: 利用可变参数来解决不同函数参数不一样的问题

import time

# 装饰器结构
def decorator(func):
    def wrapper(*args):
        print(time.time())
        func(*args)
    return wrapper


@decorator
def f2(a):
    print('This is f2 function'+a)

@decorator
def f3(b, c):
    print('This is f3 function'+b + c)

f2("一个参数")
f3("第一个参数","第二个参数")
输出结果:
PS C:\Users\Administrator\Desktop\tmp\tmp2> python .\c13.py
1550906121.4243588
This is f2 function一个参数
1550906121.4253302
This is f3 function第一个参数第二个参数

带 关键字 **keyword 的装饰器

import time

# 带关键字参数的,装饰器结构
def decorator(func):
    def wrapper(*args, **kw):
        print(time.time())
        func(*args, **kw)
    return wrapper

@decorator
def f3(b, c, **kw):
    print('This is f3 function'+b + c)
    print(kw)

f3("第一个参数","第二个参数", key1=1, key2=2,key3='abc')

执行结果:
PS C:\Users\Administrator\Desktop\tmp\tmp2> python .\c13.py
1550906412.727659
This is f3 function第一个参数第二个参数
{'key1': 1, 'key2': 2, 'key3': 'abc'}

tips:
在一些不知道参数的抽象函数中,可以使用 *args, **kw 来替代所有参数
*args是非关键字参数,用于元组tuple,**kw是关键字参数,用于字典dict

猜你喜欢

转载自blog.csdn.net/Ciellee/article/details/87891767