匿名函数知多少
一、有名函数 它是基于函数名使用 def func(): print('hello hello') func() func() func() print(func) ##hello hello ##hello hello ##hello hello 二、匿名函数 匿名函数,他没有绑定名字,使用一次即被回收,加括号既可以运行 res=(lambda x,y:x+y)(1,2) print(res) #3 三、与内置函数联用 匿名函数通常与max()、sorted()、filter()、sorted() salary_dict={ 'nick':3000, 'jason':100000, 'tank':5000, 'sean':2000 } 如果我们想从上述字典中取出薪资最高的人,我们可以使用max(),但是max()默认比较的字典的可以 1、首先将可迭代对象变成迭代器对象 2、res=next(迭代器对象),将res当作参数传给key指定函数,然后将该函数的返回值当作判断依据 salary_dict={ 'nick':3000, 'jason':10000, 'tank':5000, 'sean':2000 } print(f'max(salary-dict):{max(salary_dict)}') def func(k): return salary_dict[k] print