Map Reduce in python

Map and Reduce are two powerful methods of Python that is used to solve big logical problems, suppose you have to calculate how many routers are making error in network. So write a single function and map it with list of files. The Reducer will then count the total number of routers.

Python Code..

#import Library for Reducer

from _functools import reduce

#Created a list for numbers for experiment
lst=[1,2,3]

#Defined a function that will map on a list of numbers

def powernum(x):
return x**2

#Use of Mapper
var=list(map(powernum,lst))

print(var)

#Use of Reducer

var1=reduce(lambda x,y:x+y,var)

print(var1)

Leave a Reply