-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path48 Map, Filter & Reduce.py
More file actions
52 lines (44 loc) · 937 Bytes
/
48 Map, Filter & Reduce.py
File metadata and controls
52 lines (44 loc) · 937 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# Map, Filter & Reduce
# normal method
numbers = ['3','34','64']
for i in range(len(numbers)):
numbers[i] = int(numbers[i])
hello = numbers[2] + 1
print(hello)
# map with function
def sq(a):
return a * a
num = [2,3,6,6,7,6,3,3,2]
square = list(map(sq, num))
print(square)
# map with function
num = [2,3,6,6,7,6,3,3,2]
square = list(map(lambda x: x*x, num))
print(square)
def square(a):
return a * a
def cube1(a):
return a * a * a
func = [square, cube1]
for i in range(5):
val = list(map(lambda x:x(i), func))
print(val)
# filter
num = [2,3,6,6,7,6,3,3,2]
def is_greater_5(num):
return num > 5
gr_then_5 = list(filter(is_greater_5,num))
print(gr_then_5)
# reduce
from functools import reduce
list1 = [1,2,3,4]
num = 0
for i in list1:
num = num + i
print(num)
list1 = [1,2,3,4,7]
num = reduce(lambda x,y:x+y, list1)
print(num)
list1 = [1,2,3,4,7]
num = reduce(lambda x,y:x*y, list1)
print(num)