2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > python语言支持函数式编程_Python语言之Pyhton入门笔记函数式编程

python语言支持函数式编程_Python语言之Pyhton入门笔记函数式编程

时间:2019-11-23 23:38:48

相关推荐

python语言支持函数式编程_Python语言之Pyhton入门笔记函数式编程

本文主要向大家介绍了Python语言之Pyhton入门笔记函数式编程,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。

一,匿名函数

def add(x,y)

return x+y

print(add(2,3))

f=lambda x,y:x+y #匿名函数需要lambdb来指定,lambda后直接跟参数,然后是:冒号,冒号后是表达式,只能是中表达式。当要引用匿名函数的时候,要赋值给变量才可以。

print(f(1,2))

二, 三元表达式

条件为真时返回的结果 if 条件判断 else 条件为假时返回的结果

x if x>y else y

x=2

y=1

f=x if x>y else y #因为是表达式,所以要被赋值使用。

print(r)

三,map的使用

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

def square(x):

reture x*x

r=map(square,list_x)

print(list(r))

四,map与lambda相结合使用

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

r=map(lambda x:x*x,list_x)

print(list(r)) #例三与例四相同

五,map与lambda相结合,多参数

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

list_y=[1,2,3,4,5,6]

r=map(lambda x,y:x*x+y,list_x,list_y)

print(list(r))

六,编程模型map/reduce 映射 归约

from functools import reduce

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

r=reduce(lambda x,y:x+y,list_x,10) #reduce为连续计算,连续调用lambda.10参数可以忽略。如果忽略计算方式为,第一次,x=1,y=2,相加算计为3,第二次x=3,y=3,相加为6。第三次x=6,y=4相加为10,第四次,,,,,以次相加计算,一至加到6。当10不忽略的时候,10为x的初始值,第一次为x=10,y=1相加,以次计算......

print(r)

注:reduce()函数内做为参数的函数lambda()必须要有两个参数。

七,过滤,filter

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

r=filter(lambda x:True if x==1 else False,list_x) #函数filter()要求lambda一定要返回一个真假,或者返回一个能代表真假的,此名也可写为:r=filter(lambda x:x,list_x),因为x为1是真,为0是假

print(list(r))

八,装饰器

8.1)装饰器前奏

要求在每个函数前都要打印出时间

import Time

def f1():

print('This is a function')

def f2():

print('This is a function')

def print_current_time(fuc):

print(time.time())

fuc()

print_current_time(f1)

print_current_time(f2)

8.2)装饰器前奏

import time

def decorator(fuc):

def wrpper():

print(time.time())

fuc()

return wrpper

def f1():

print('This is a function')

def f2():

print('This is a function')

f=decorator(f1)

f()

8.3)装饰器

import time

def decorator():

def wrapper():

print(time.time())

fuc()

return wrapper

@decorator #装饰器 语法堂

def f1():

print('This is a function')

f1()

8.4)装饰器,参数

import time

def decorator():

def wrapper(func_name):

print(time.time())

fuc(func_name)

return wrapper

@decorator #装饰器 语法堂

def f1(func_name):

print('This is a function'+func_name)

f1()

8.5)装饰器,多参数

import time

def decorator():

def wrapper(*args): #可变参数,args可为任一变量

print(time.time())

fuc(*args)

return wrapper

@decorator #装饰器 语法堂

def f1(func_name):

print('This is a function'+func_name)

@decorator

def f2(func_name1,func_name2)

print('This is a function'+func_name1)

print('This is a function'+func_name2)

f1(test_func)

f2(test_func1,test_func2)

8.6装饰器,关键词参数

import time

def decorator():

def wrapper(*args,**kw): #可变参数,args可为任一变量 。关键词参数kw也是任一变量

print(time.time())

fuc(*args,**kw)

return wrapper

@decorator #装饰器 语法堂

def f1(func_name):

print('This is a function'+func_name)

@decorator

def f2(func_name1,func_name2)

print('This is a function'+func_name1)

print('This is a function'+func_name2)

@decorator

def f3(func_name1,func_name2,**kw) #**kw关键词参数

print('This is a function'+func_name1)

print('This is a function'+func_name2)

print(kw)

f1(test_func)

f2(test_func1,test_func2)

f3(test_func1,test_func2,a=1,b=2,c='1,2,3')

本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。