-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter map reduce.py
59 lines (38 loc) · 1.29 KB
/
filter map reduce.py
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
print("Program to impliment the filter, map and reduce methods : \n")
#-------------------------------------------------------
#Importing the reduce function from Functools module:
from functools import reduce
#--------------------------------------------------------
#Filter, map and reduce take a function and a iteratable :
#Filter Method:
def even(n):
return n % 2 == 0
list1 = [2,13,42,4,524,331,452,321,22]
list_even = []
print(list1)
print(list_even)
for i in list1:
if i % 2 == 0:
list_even.append(i)
for i in list1:
y = even(i)
if y == True:
list_even.append(i)
print(list_even)
list_even = list(filter(even, list1))
#usually used with big data :
#using Inline Lambda function instead of normal function:
list_even2 = list(filter(lambda x : x % 2 == 0, list1))
list_odd = list(filter(lambda x : x % 2 != 0, list1))
print(list1)
print(list_even)
print(list_even2)
print(list_odd)
#---------------------------------------------------------
#Map Function :
list_even_mapped = list(map(lambda x : x * 2, list_even))
print(list_even_mapped)
#----------------------------------------------------------
#Reduce Function :
sum_of_list_even_mapped = reduce(lambda x ,y : x + y, list_even_mapped)
print(sum_of_list_even_mapped)