Python匿名函数lambda、filter函数、map函数、reduce函数用法详解




>>> range(1,100)
range(1, 100)
>>> x=range(1,100)
>>> for i in range(1,100):
	print (i)

	
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
>>> for i in range(1,100):
	if i%3==0:
		print(i)

		
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
>>> fun_b=filter(lambda x: not(x%3),range(0,100))
>>> fun_b
<filter object at 0x0000000002E775C0>
>>> for x in fun_b:
	print(x)

	
0
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
>>> x1=[1,2,3,4,5]
>>> x2=[6,7,8,9,10]
>>> zip(x1,x2)
<zip object at 0x0000000002E97748>
>>> for i in zip(x1,x2):
	print(i)

	
(1, 6)
(2, 7)
(3, 8)
(4, 9)
(5, 10)
>>> list(zip(x1,x2))
[(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
>>> list(map(lambda x,y:[x,y],(1,2,3,4,5),(6,7,8,9,10)))
[[1, 6], [2, 7], [3, 8], [4, 9], [5, 10]]
>>> map(lambda x:x*2,8)
Traceback (most recent call last):
  File "<pyshell#23>", line 1, in <module>
    map(lambda x:x*2,8)
TypeError: 'int' object is not iterable
>>> map(lambda x:x*2,[8,9])
<map object at 0x0000000002EE19E8>
>>> reslut = filter(lambda num: num % 2, range(10))
	 
>>> print(list(reslut))
	 
[1, 3, 5, 7, 9]
>>> list((map(lambda x:x*2,[8,9])))
	 
[16, 18]
>>> foo = range(0,10)
>>> list(map(lambda x:x*2+1,range(0,10)))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> list(filter(lambda x:x%3==0,range(0,10)))
[0, 3, 6, 9]
>>> list(filter(lambda x:not x%2,range(1,100)))



>>> list(map(lambda x:x%3==0,range(0,10)))
[True, False, False, True, False, False, True, False, False, True]
>>> list((map(lambda x:x*2,('i love you'))))
['ii', '  ', 'll', 'oo', 'vv', 'ee', '  ', 'yy', 'oo', 'uu']
>>> list((map(lambda x:x*2,['i love you'])))
['i love youi love you']
>>> map(lambda x:not x%2,range(1,100))
<map object at 0x0000000002E65A58>
>>> list(map(lambda x:not x%2,range(1,100)))
[False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]
>>> list(filter(lambda x:not x%2,range(1,100)))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
>>> list(filter(lambda x:x%2,range(1,100)))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
>>> 


>>> list(map(lambda x:x%3==0,range(0,10)))
[True, False, False, True, False, False, True, False, False, True]
>>> list((map(lambda x:x*2,('i love you'))))
['ii', '  ', 'll', 'oo', 'vv', 'ee', '  ', 'yy', 'oo', 'uu']
>>> list((map(lambda x:x*2,['i love you'])))
['i love youi love you']
>>> map(lambda x:not x%2,range(1,100))
<map object at 0x0000000002E65A58>
>>> list(map(lambda x:not x%2,range(1,100)))
[False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]
>>> list(filter(lambda x:not x%2,range(1,100)))
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]
>>> list(filter(lambda x:x%2,range(1,100)))
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81, 83, 85, 87, 89, 91, 93, 95, 97, 99]
>>> 

参考:#https://www.bbsmax.com/A/Vx5MwE6azN/

>>> B = lambda :True

>>> B()
True
>>> two_sum = lambda x, y: x + y
>>> two_sum(1,2)
3
>>> def two_sum(x,y):
	return x+y

>>> two_sum(1,2)
3
>>> 
>>> sum_with_100 = lambda x, y=100: x + y

>>> def sum_with_100(x, y=100):
	return x + y


>>> sum_with_100(200)
300
>>> two_sum = (lambda x, y: x + y,(3, 4))
>>> two_sum
(<function <lambda> at 0x00000000003C1E18>, (3, 4))
>>> list(two_sum)
[<function <lambda> at 0x00000000003C1E18>, (3, 4)]
>>> for i in two_sum:
	print(i)

	
<function <lambda> at 0x00000000003C1E18>
(3, 4)
>>> def sum(x):
 return lambda y: x + y

>>> sum_with_100 = sum(100)
>>> sum_with_100
<function sum.<locals>.<lambda> at 0x0000000002ED30D0>
>>> for i in sum_with_100:
	print(i)

	
Traceback (most recent call last):
  File "<pyshell#31>", line 1, in <module>
    for i in sum_with_100:
TypeError: 'function' object is not iterable

>>> result = sum_with_100(200)
>>> print(result)
300
>>> lower=lambda x,y:x if x<y else y
>>> lower(7,100)
7
>>> d = [{"order":3},{"order":1},{"order":2}]

>>> d.sort(key=lambda x:x['order'])
>>> d
[{'order': 1}, {'order': 2}, {'order': 3}]
>>> def my_add(x,y):
  z=x+y

  
>>> my_add(1,2)
>>> 
>>> print(my_add(1,2))
None
>>> def my_add(x,y):
  z=x+y
  return z

>>> my_add(1,2)
3
>>> import functools
>>> print(functools.reduce(lambda x,y:x+y,range(10)))
45
>>> print(functools.reduce(lambda x,y:x*10+y,[1,2,3]))
123
>>> print(functools.reduce(lambda x,y:x+y,range(5)))
10
>>> 

参考:
https://www.jb51.net/article/141395.htm
https://www.jb51.net/article/177265.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值