特殊函数
lambda
基本格式lam = lambda x,y: x+y
type(lam)
<class 'function'>
lam(2,3)
5
n = range(-5,5)
lam1 = lambda n:n>0
[lam1(i) for i in n]
[False, False, False, False, False, False, True, True, True, True]
map
map把10以内的数字加一
[i+1 for i in range(0,10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[(lambda x:x+1)(i) for i in range(10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = map(lambda x:x+1, range(0,10))
m
<map object at 0x00000208AD4FFDC0>
list(m)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
a = [1,2,3]
b = [4,5.6]
[x+y for x,y in zip(a,b)]
[5, 7.6]
r = map(lambda x,y:x+y, a, b)
list(r)
[5, 7.6]
map即把后面的参数传入第一个参数函数中,作为参数运算,最终得出map对象
filter
filter使用函数过滤参数
n = range(-5,5)
f = filter(lambda x:x>0,n)
f
<filter object at 0x00000208AD4B2160>
list(f)
[1, 2, 3, 4]
网友评论