0% found this document useful (0 votes)
7 views

map_reduce_filter_lambda_generator

Map filter and reduce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

map_reduce_filter_lambda_generator

Map filter and reduce
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

MAP, REDUCE ,FILTER

Lambda, Generator
Lambda Function or Anonymous Function
● Function that is created using lambda keyword not using def keyword.
● Lambda function are just needed where they have been created and
can be used anywhere a function required.
● Lambda function contains only a single line .

● Example : sum=lambda x, y : x+y


print(“Sum=”, sum(3,5))
Lambda Functions : Key points
● Lambda function have no name
● Lambda function can take any number of arguments
● Lambda function just return one value
● Lambda function definition does not have any explicit return statement but it
always contains an expression which is returned
● They are a one-line version of a function and hence cannot contain multiple
expression
● Lambda function can not access global variable
● You can use Lambda function in normal function
Lambda Syntax
lambda.py
1 #Example 1
2 square_func = lambda x : x**2
3 square_func(4) #return: 16
4
5 #Example 2
6 close_enough = lambda x, y : abs(x – y) < 3
7 close_enough(2, 4) #return: True
8
9 #Example 3
0 def get_func(n) :
1 return lambda x : x * n + x % n
2 my_func = get_func(13)
3 my_func(4) #return: 56
GENERATORS
• Are special functions that help to define user defined iterators
• The generator is a construct that generates a dynamic sequence
and it generates an iterator
• They are Functions that return a sequence of values
• It is written like an ordinary function but it uses ‘yield’
statement
● The generated object is remembered by the Generator.
● These Generators do not compute untill they are
needed. This is called lazy evaluation i.e. only on
request computation is done
● Generators can be used to generate values infinitely .
● Therefore they can be used to model an infinite data
sequence with no end. These are stream data
Example - Odd number Generator
def generate_odd(m,n):
while(m < n):
if m% 2 != 0:
yield m
m = m+1

for odd in generate_odd(0,100):


print(odd, end = ‘,’)
print()
OUTPUT
1,3,5,7,9......99,
Example FibonacciGenerator
def fib(n):
x=1
y=1
for i in range(n):
yield(x)
x,y = y, x+y

fib(10)
• Sometimes we want to pass an entire
function as a parameter
• Python has functions as first-class citizens,
so you can do this
• You simply pass the functions by name
Higher-Order Functions

• A higher-order function is a function that takes another


function as a parameter
• They are “higher-order” because it’s a function of a function
• Examples
–Map
–Reduce
–Filter
• Lambda works great as a parameter to higher-order
functions if you can deal with its limitations
Map
map(function, iterable, ...)

• Map applies function to each element of iterable and


creates a list of the results
• You can optionally provide more iterables as parameters
to map and it will place tuples in the result list
• Map returns an iterator which can be cast to list
Map Example
Example

1 nums = [0, 4, 7, 2, 1, 0 , 9 , 3, 5, 6, 8, 0, 3]
2
3 nums = list(map(lambda x : x % 5, nums))
4
5
print(nums)
6
7 #[0, 4, 2, 2, 1, 0, 4, 3, 0, 1, 3, 0, 3]
Example
items = [ “copper”, “Zinc”, “Nickel”]
items_new = list(map(str.upper, items))
print(items_new)

OUTPUT

[‘COPPER’, ‘ZINC’, ‘NICKEL’]


Map Problem
Goal: given a list of three dimensional points in the
form of tuples, create a new list consisting of the
distances of each point from the origin

Loop Method:
- distance(x, y, z) = sqrt(x**2 + y**2 + z**2)
- loop through the list and add results to a new list
Solution
1 from math import sqrt
2
3 points = [(2, 1, 3), (5, 7, -3), (2, 4, 0), (9, 6, 8)]
4
5 def distance(point) :
6 x, y, z = point
7 return sqrt(x**2 + y**2 + z**2)
8
9 distances = list(map(distance, points))
Filter iterable)
filter(function,
• The filter runs through each element of iterable (any
iterable object such as a List or another collection)
• It applies function to each element of iterable
• If function returns True for that element then the
element is put into a List
• This list is returned from filter in versions of python under
3
• In python 3, filter returns an iterator which must be cast
to type list with list()
Filter Example

Example

1 nums = [0, 4, 7, 2, 1, 0 , 9 , 3, 5, 6, 8, 0, 3]
2
3 nums = list(filter(lambda x : x != 0, nums))
4
5 print(nums) #[4, 7, 2, 1, 9, 3, 5, 6, 8, 3]
6
Example - to find Palindromes in a list of strings
palindrome_list = [“Top”, “nun”, “tattarrattat”, “advice”, “noon”]
print(palindrome_list)

result = list(filter(lambda a: (a ==””.join(reversed(a))), palindrome_list)

print(result)
Ex: to generate Even numbers
def even(n):
if(n % 2 == 0)
return TRUE
else:
return FALSE

lst = [2,10,14,9,50]

even_lst = filter(even, lst)


print(list(even_lst))
Reduce
reduce(function, iterable[,initializer])

• Reduce will apply function to each element in iterable along with


the sum so far and create a cumulative sum of the results
• In short reduces a set of values to single number
• function must take two parameters
• If initializer is provided, initializer will stand as the first argument in
the sum
• Unfortunately in python 3 reduce() requires an import statement
• from functools import reduce
Reduce Example

Example

1 nums = [1, 2, 3, 4, 5]
2
3 sum = reduce(lambda x, y : x+y, nums,10)
4
5 Print(sum)
6
7 >> 25
Reduce Example

Example

1 nums = [9, 20, 40,60,3]


2
3 min_element = reduce(min,list_numbers)
4
5 Print(min_element)
6
7 >> 3
Reduce Problem
Goal: given a list of numbers I want to find the
average of those numbers in a few lines using
reduce()

For Loop Method:


- sum up every element of the list
- divide the sum by the length of the list
Reduce Problem

Solution

1 nums = [92, 27, 63, 43, 88, 8, 38, 91, 47, 74, 18, 16,
29, 21, 60, 27, 62, 59, 86, 56]
2
3 sum = reduce(lambda x, y : x + y, nums) / len(nums)
4
MapReduce

Problem: Given an email how do you tell if it is spam?

- Count occurrences of certain words. If they


occur too frequently the email is spam.
MapReduce
map_reduce.py

1 email = ['the', 'this', 'annoy', 'the', 'the', 'annoy']


2
3 def inEmail (x):
if (x == "the"):
4 return 1;
5 else:
return 0;
6
spam_val = list(map(inEmail, email)) #[1, 0, 0, 1, 1, 0]
7
8 reduce((lambda x, xs: x + xs), spam_val) #3
9
1
0

You might also like