[what] Python Map, Filter and Reduce Functions

 






Map, Filter and Reduce are functions that accepts an input function as a parameter and return another function as a result; they represent the concept of Functional Programming. Often the input function is used to process iterable objects in a single statement which is called as Lambda in Python.

Map function takes an input function and an iterable object items, and then MAPS them, producing a new collection of values.

map(function, iterable(s))

Filter function takes an input function and an iterable object items, and then FILTERS those items that satisfy certain conditions i.e. returns True. 

filter(function, iterable(s))

Reduce() takes an input function and a sequence (or an iterable object), and then REDUCES or accumulates the processing result into a single value. Reduce() works by calling the input function for the first two items in the sequence. The result of the function processing is then fed into the next call (combining the input function and the third item). This process repeats until the last item in the sequence.

reduce(function, sequence[, initial])



print ("\n====================\n")
textSource='''
Twinkle, twinkle, little star.
How I wonder what you are.
Up above the world so high.
Like a diamond in the sky.
'''
print("Source text:\n{}".format(str(textSource)))


print ("\n====================\n")
# replace comma with no char
# replace new line with no char
# replace dot with space char
# lower all char
# split by space char
# sort
listWord=sorted(textSource.replace(",","").replace("\n","").replace("."," ").lower().split())
print("Preprocessed text:\n{}".format(str(listWord)))

print ("\n====================\n")
listStopWord=['a','above','are','how','i','in','so','the','up','what','you']
print("Stop words:\n{}".format(str(listStopWord)))

print ("\n====================\n")
# use lambda to filter words that are not stop words
listFilteredWord  = list(filter(lambda element:element not in listStopWord, listWord))
print("Filtered words (filter lambda):\n{}".format(str(listFilteredWord)))

print ("\n====================\n")
# create set of unique words from list of all words
setWord=set(listFilteredWord)
print("Unique words:\n{}".format(str(setWord)))

print ("\n====================\n")
dictVectorizedWord=dict(enumerate(setWord,1))
print("Word vector:\n{}".format(str(dictVectorizedWord)))

print ("\n====================\n")
# use lambda to map new values

listWordSize=dict(map(lambda element:[element,len(element)], setWord))

print("Word size (map lambda):\n{}".format(str(listWordSize)))

print ("\n====================\n")
# use lambda to accumulate values

def countElement(acc,elm):
  if(acc):
    if acc.has_key(elm):
      acc[elm]=acc[elm]+1
    else:
      acc[elm]=1  
  else:
    acc[elm]=1
  return acc

dictWordCount=reduce(lambda accumulator,element:countElement(accumulator,element),listFilteredWord,{})
print("Word frequencies (reduce lambda):\n{}".format(str(dictWordCount)))

Post a Comment

0 Comments