匿名函数
# 匿名函数 别称 : 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(‘找到了‘)
原文地址:https://www.cnblogs.com/Godisgirl/p/10012914.html
时间: 2024-10-08 18:59:53