def add(x,y): return x+y #对应的匿名函数 # add = lambda x,y:x+y print(add(89,23)) #112
面试题1
现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}] 看到匿名函数---->想到内置函数 zip ret = zip((('a'),('b')),(('c'),('d'))) res = map(lambda tup :{tup[0]:tup[1]},ret) print(list(res))
面试题2
def multipliers(): return [lambda x:i*x for i in range(4)] print([m(2) for m in multipliers()]) #[6, 6, 6, 6] def multipliers(): return (lambda x:i*x for i in range(4)) print([m(2) for m in multipliers()]) #[0, 2, 4, 6]