021函数:lambda表达式

lambda函数

就不用给函数起名字

>>> fun1()
25
>>> def ds(x):
	return 2* x + 1

>>> ds(5)
11
>>> g = lambda x : 2 * x + 1
>>> g(5)
11

filter()过滤器

>>> list(filter(None, [1,0,False,True]))
[1, True]

两个BIF

filter()

>>> def odd(x):
	return x % 2

>>> temp = range(10)
>>> show = filter(odd,temp)
>>> list(show)
[1, 3, 5, 7, 9]
>>> list(filter(lambda x :x % 2,range(10)))
[1, 3, 5, 7, 9]

map()

>>> list(map(lambda x : x * 2 ,range(10)))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

课后作业

在这里插入图片描述
0、

>>> lambda x, y = 3:x * y

1、

>>> def a(x):
	if x % 2 :
		return x
	else:
		None

3、

>>> list(filter(lambda n : not(n % 3),range(1,100)))
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

4、

发布了42 篇原创文章 · 获赞 0 · 访问量 289

猜你喜欢

转载自blog.csdn.net/qq_43169516/article/details/103718483
今日推荐