Saturday, October 14, 2017

Map Reduce in Python

BDSP_MapReduce
In [10]:
# Praxis Business School 
# Prithwis Mukerjee
#
# Data Structures in Python
# there are some changes that are necessary for these codes to work in Python 3
# range, filter, map return objects that are not lists … hence we need to cast them into lists
# reduce has been replaced with functools.reduce
# these changes are shown in red

#http://stackoverflow.com/questions/12319025/filters-in-python
#http://stackoverflow.com/questions/13638898/how-to-use-filter-map-and-reduce-in-python-3
print("Hello to Data Structues")
Hello to Data Structues
In [11]:
print("Filter Functions ................................")

pList1 = list(range(1,10))
print(pList1)

def pEven(x) : return x%2 == 0
pList2 = list(filter(pEven,pList1))
print(pList2)

pList3 = ["Ram", "RamPM", "Shyam", "PMShyam", "Sita", "SiPMMta"]
print(pList3)

def hasPM(x) : return "PM" in x
pList4 = list(filter(hasPM, pList3))
print(pList4)
Filter Functions ................................
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[2, 4, 6, 8]
['Ram', 'RamPM', 'Shyam', 'PMShyam', 'Sita', 'SiPMMta']
['RamPM', 'PMShyam', 'SiPMMta']
In [12]:
# Lambda functions
print("Lambda Functions ................................")

pList2a = list(filter (lambda x : x%2 == 0, pList1))
print(pList2a)

pList4a = list(filter(lambda x: "PM" in x, pList3))
print(pList4a)
Lambda Functions ................................
[2, 4, 6, 8]
['RamPM', 'PMShyam', 'SiPMMta']
In [13]:
# Map
print("Map Functions ................................")

pList5 = list(map(lambda x : x*x, pList1))            # square each number in the list
print(pList5)

pList6 = list(map(lambda x : "Y"+x, pList3))          # appends the char Y to each string in the list
print(pList6)

pList7 = map(lambda x : len(x), pList3)         # returns the number of characters in each string of the list
print(pList7)

pList8 = map(lambda x : (x, len(x)),pList3)     # returns a list of tuples
print(pList8)
Map Functions ................................
[1, 4, 9, 16, 25, 36, 49, 64, 81]
['YRam', 'YRamPM', 'YShyam', 'YPMShyam', 'YSita', 'YSiPMMta']
<map object at 0x7fc0940605c0>
<map object at 0x7fc0867c6748>
In [14]:
# Reduce
print("Reduce Functions ................................")

import functools
pVal1 = functools.reduce(lambda x,y: x+y, pList1)         #   adding the numbers in a list
print(pVal1)

pVal2 = functools.reduce(lambda x,y: x+y, pList3)         # concatenating string in a list
print(pVal2)

pVal3 = functools.reduce(lambda x,y: (x[0]+y[0], x[1]+y[1]), pList8)  #   adding and concatenating elements in a list
print(pVal3)
Reduce Functions ................................
45
RamRamPMShyamPMShyamSitaSiPMMta
('RamRamPMShyamPMShyamSitaSiPMMta', 31)
In [ ]:
 

No comments:

Post a Comment