-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda.py
More file actions
36 lines (27 loc) · 663 Bytes
/
Copy pathlambda.py
File metadata and controls
36 lines (27 loc) · 663 Bytes
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
# lambda arguemnts: expression
add10 = lambda x: x + 10
print(add10(5))
# same as
def add10_func(x):
return x + 10
print(add10_func(5))
mult = lambda x,y: x*y
print(mult(2,7))
points2d = [(1,2), (15,1), (5,-1), (10,4)]
points2d_sorted = sorted(points2d, key=lambda x: x[1])
print(points2d_sorted)
# map function
a = [1,2,3,4,5]
b = map(lambda x: x*2, a)
print(list(b))
# filter(func, seq) function
a = [1,2,3,4,5,6]
b = filter(lambda x: x%2==0, a)
print(list(b))
c = [x for x in a if x%2 ==0] # same thing without lambda
print(c)
# reduce(func, seq)
from functools import reduce
a = [1,2,3,4]
product_a = reduce(lambda x,y: x*y, a)
print(product_a)