- lambda的传参数是行参
- 定义:变量名=
lambda参数(参数1参数2..) :返回的表达式
调用:变量名(参数)
# 普通函数def add (x,y):return x+y# 匿名函数f=lambda x ,y:x+yprint(add(1,2))print(f(1,2))
my_list=[('a',7),('b',3),('c',2),('d',6),]# sorted 可以对所有可迭代的对象进行排序操作,默认升序,即是不要reverseprint(sorted(my_list,key=lambda x:x[1],reverse=True))print(sorted(my_list))
my_list=[1,2,3,4,5,6]res = []#使用for循环遍历for i in my_list:res.append(i**2)print(res)# 不使用for循环,使用匿名函数,map映射到lambdanum=map(lambda x:x**2 ,my_list)print(list(num))
my_list1 = [1, 2, 3, 4, 5, 6]my_list2 = [1, 2, 3, 4, 5, 6]res = []sum1 = map(lambda x, y: x+y, my_list1, my_list2)print(list(sum1))
reduce
let sum =([a,b])=>a+b;console.log(sum([10,20]));let arr=[10,20];let res=arr.reduce((a,b)=>{console.log(a+b);});
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号