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頻道!
總結
以上是生活随笔為你收集整理的python语言支持函数式编程_Python语言之Pyhton入门笔记函数式编程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 词云图 - WorldCloud - P
- 下一篇: python中如何输入矩阵_python