>>> g = lambda x,y:x+y #x+y,并返回结果 >>> g(3,4) 7 >>> (lambda x:x**2)(4) #返回4的平方 16
>>> lamb = [ lambda x:x,lambda x:x**2,lambda x:x**3,lambda x:x**4 ] >>> for i in lamb: ... print i(3), ... 3 9 27 81
>>> items = [1,2,3,4,5] >>> squared = [] >>> for i in items: ... squared.append(i**2) ... >>> squared [1, 4, 9, 16, 25] >>> def sqr(x): return x**2 ... >>> map(sqr,items) [1, 4, 9, 16, 25] >>> map(lambda x: x**2, items) [1, 4, 9, 16, 25] >>> [ x**2 for x in items ] #这个我最喜欢了,一般情况下速度足够快,而且可读性强 [1, 4, 9, 16, 25]
>>> lst1 = [1,2,3,4,5] >>> lst2 = [6,7,8,9,0] >>> map(lambda x,y: x+y, lst1,lst2) #将两个列表中的对应项加起来,并返回一个结果列表 [7, 9, 11, 13, 5]
>>> lst1 = [1,2,3,4,5] >>> lst2 = [6,7,8,9,0] >>> lst3 = [7,8,9,2,1] >>> map(lambda x,y,z: x+y+z, lst1,lst2,lst3) [14, 17, 20, 15, 6]
>>> reduce(lambda x,y: x+y,[1,2,3,4,5]) 15
>>> reduce(lambda x,y: x+y,[1,2,3,4,5],100) 115
>>> numbers = range(-5,5) >>> numbers [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] >>> filter(lambda x: x>0, numbers) [1, 2, 3, 4] >>> [x for x in numbers if x>0] #与上面那句等效 [1, 2, 3, 4] >>> filter(lambda x: x > 3, [1,2,3,4,5]) [4,5]
filter(...) filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list.
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有