python中的關鍵字---4(匿名/遞迴函式)
阿新 • • 發佈:2018-11-24
匿名函式
# 匿名函式 別稱 : lambda表示式
# 函式,沒有名字
# def wahaha(n):
# return n**2
# print(wahaha.__name__)
# qqxing = lambda n : n**2
# print(qqxing.__name__)
# ret = qqxing(8)
# print(ret)
# f = lambda *args:sum(args)
# ret = f(1,2,3,4,5)
# print(ret)
# 寫匿名函式 : 接收兩個引數,返回較大值(不用max內建函式)
# f1 = lambda a,b : max(a,b)
# f2 = lambda a,b : a if a>b else b
# 匿名函式
# 定義
# 匿名函式的記憶體地址 = lambda 引數1,引數2 : 返回值/返回值的表示式
# 呼叫
# 接收返回值 = 匿名函式的記憶體地址(引數)
# dic={'k1':10,'k2':100,'k3':30}
# def func(k):
# return dic[k]
#
# m = max(dic,key = func)
# print(m)
# m = max(dic,key = lambda k:dic[k])
# print(m)
# 內建函式+匿名函式
l1 = [1,5,7,4,8]
# 每一個數的平方%2
# ret = map(lambda n : n**2%2 ,l1)
# print(list(ret))
l2 = [3,10,20,50,22,7]
# 找到大於20的所有數
# ret = filter(lambda n : n>20,l2)
# print(list(ret))
l3 = [{'name':'www','age':83},{'name':'asd','age':38}]
# 1.找到年齡大於40的值 filter
# 2.將列表中的項按照年齡從小到大排序 sorted
# 3.給列表中的每一個人的年齡+1
# 4.給列表中名叫www的人的年齡+1 三元運算子+字典拼接
# 1
# def func(dic):
# return dic['age']>40
# ret = filter(lambda dic:dic['age']>40,l3)
# print(list(ret))
# 2
# def func(dic):
# return dic['age']
# ret = sorted(l3,key=lambda dic:dic['age'])
# print(ret)
# 3 lambda 並不能取代函式
# def func(dic):
# dic['age'] += 1
# return dic
# ret = map(lambda dic : {'name':dic['name'],'age':dic['age']+1},l3)
# print(list(ret))
# 4
# 現有兩個元組(('a'),('b')),(('c'),('d')),
# 請使用python中匿名函式生成列表[{'a':'c'},{'b':'d'}]
# t1 = (('a'),('b'))
# t2 = (('c'),('d'))
# print(t1,t2)
# ret = zip(t1,t2)
# def func(tup):
# return {tup[0]:tup[1]}
# map(func,ret)
# iterator1 = map(lambda tup:{tup[0]:tup[1]} ,ret)
# print(list(iterator1))
匿名函式lambda常配合內建函式組成表示式,進行簡單的判斷計算,節省程式碼,邏輯性強
遞迴函式
# 1.遞迴函式的定義 :一個函式在執行的過程中呼叫了自己
# 2.遞迴在python中的最大深度 : 1000/998
# 3.遞迴的最大深度是可以自己修改的,但是不建議你修改
# 停不下來的遞迴函式早晚會報錯
# 瞭解遞迴/正確的遞迴函式
# n = 1,age = q(n+1) -2
# n = 2,age = q(n+1) -2
# n = 3,age = q(n+1) -2
# n = 4,age = q(n+1) -2
# n = 5,age = 18
# def q(n):
# if n == 1:
# age = q(n + 1) - 2
# return age
# elif n == 2:
# age = q(n + 1) - 2
# return age
# elif n == 3:
# age = q(n + 1) - 2
# return age
# elif n == 4:
# age = q(n + 1) - 2
# return age
# elif n == 5:
# return 18
# ret = q(1)
# print(ret)
# return 能夠幫助你停止遞迴,並且把最內層的結果一層一層的返回到最外層
# 在最內層函式的返回值,返回的時候永遠是離最內層最近的上一層才能接收到返回值
# def q(1):
# if 1 == 1:
# age = 12 - 2
# return 10
#
# def q(2):
# elif 2 == 2:
# age = 14 - 2
# return 12
#
# def q(3):
# elif n == 3:
# age = 16 - 2
# return 14
#
# def q(4):
# elif n == 4:
# age = 16
# return age
#
# def q(5):
# elif n == 5:
# return 18
# def q(n):
# if n < 5:
# age = q(n + 1) - 2
# return age
# elif n == 5:
# return 18
# ret = q(1)
# print(ret)
二分查詢法
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
# aim = 66
# # l中找到中間的值 和 66 對比
# def search(l):
# mid = len(l)//2
# mid_value = l[mid]
# if aim > mid_value:
# start = mid+1
# new_l = l[start:]
# search(new_l)
# elif aim < mid_value:
# end = mid
# new_l = l[:end]
# search(new_l)
# else:
# print('找到了',mid,l[mid])
#
# search(l)
# 遞迴過程
# def search(l): # l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
# mid = len(l)//2 # 25//2 = 12
# mid_value = l[mid] # l[12] = 41
# if aim > mid_value: # 66 > 41
# start = mid+1 # 12+1 = 13
# new_l = l[start:] # new_l = [42,43,55,56,66,67,69,72,76,82,83,88]
# search(new_l) # search(new_l)
# elif aim < mid_value:
# end = mid
# new_l = l[:end]
# search(new_l)
# else:
# print('找到了')