==== additional functions ======
map(), reduce() and filter()
========================================================
1) map()
* This function has two parameters:
- function (lambda expression)
- sequence (single or multiple sequences)
* This function maps each element of specified sequence
with specified function.
* This will perform internal iterations and pass each
element of specified sequence to specified function.
* The map() function returns a sequence as parameter.
* Refer following program:
list1 = list(range(1, 11))
list2 = []
for val in list1:
list2.append(val*val)
print(list2)
* Here we need to apply a for-loop to iterate on list elements.
* Refer following another example:
def square(n):
return n*n
list1 = list(range(1, 11))
list2 = []
for val in list1:
list2.append( square(val) )
print(list2)
* Both above programs can be written with the help of
map() function, as follows:
def square(n):
return n*n
list1 = list(range(1, 11))
list2 = list( map(square, list1) )
print(list2)
* The map() will pass each element of "list1" as parameter
of square() and directly returns answer sequence.
* So that, we don't need to use a loop and iterate on
sequence elements.
*** Refer following another example:
list1 = list( range(1, 11) )
list2 = list( range(11, 21) )
list3 = []
i = 0
while i<len(list1):
list3.append(list1[i] * list2[i])
i = i + 1
print(list3)
* In this example, we need multiplication of elements of
two lists.
* This can also be prepared by using function.
def multiply(a,b):
return a*b
list1 = list( range(1, 11) )
list2 = list( range(11, 21) )
list3 = []
i = 0
while i<len(list1):
list3.append( multiply(list1[i], list2[i]) )
i = i + 1
print(list3)
* The same program can be written with the help of map()
function.
* The rule is, we must pass number of sequence as per
the number of parameters of mapped function.
def multiply(a,b):
return a*b
list1 = list( range(1, 11) )
list2 = list( range(11, 21) )
list3 = list( map( multiply, list1, list2) )
print(list3)
* The multiply function has 2 parameters. Therefore we
passed two sequences.
* We can also directly pass a lambda expression as parameter
to map() function.
list1 = list( range(1, 11) )
list2 = list( range(11, 21) )
list3 = list( map( lambda a,b : a*b, list1, list2) )
print(list3)
===================================================
===================================================
2. reduce():
* This function requires two parameters:
- function (lambda expression is also allowed)
- sequence (exact one sequence)
* This function returns single result/answer (not sequence)
* This function is in module "functools"
* Refer following program:
list1 = list( range(1, 11) )
print(list1)
s = 0
for val in list1:
s = s + val
print(s)
* This obtains sum of all elements of list1.
* Refer following another program:
from functools import reduce
def add(x,y):
return x+y
list1 = list( range(1, 11) )
s = reduce(add, list1)
print(s)
* Refer following program to obtain largest elements from
list1.
list1 = [41, 25, 63, 84, 20, 74, 25, 37, 23]
lar = list1[0]
i = 1
while i<len(list1):
if list1[i] > lar:
lar = list1[i]
i = i + 1
print(lar)
* The same program can be done by using reduce() function.
from functools import reduce
def large(x,y):
return x if x>y else y
list1 = [41, 25, 63, 84, 20, 74, 25, 37, 23]
lar = reduce(large, list1)
print(lar)
* This function also has third parameter. It is the value
of initializer of expression/operation.
* This function don't have rule "we have to pass number
of sequences as per number of parameters of function."
* This function requires exact one parameter.
* It is also valid to send a lambda expression as first
parameter of reduce() function.
* Prefer reduce() when we want to perform any operation
on every elements of single sequence.
===============================================
3. filter():
* This function requires 2 parameters:
- function (or lambda expression)
- sequence
* While preparing a function/lambda expression for this
operation, that function must return a boolean result.
* This filter() function returns a sequence.
* This function will filter the values whose result/return
is FALSE. And it will collect the values whose result/
return is TRUE.
* Refer following program:
def check(x):
return True if x%3==0 else False
list1 = [15, 20 ,35, 71, 52, 39, 12, 99]
list2 = list( filter(check, list1) )
print(list2)
* In above example, the filter() function filters the values
whose return was False. And collected the values whose
return was True.
* The above program can be also prepared, by using a loop
and manually collecting values. Refer following program.
list2 = []
list1 = [15, 20 ,35, 71, 52, 39, 12, 99]
for val in list1:
if val%3==0:
list2.append(val)
print(list2)
* Both above programs results same.
* Refer following another program where we will filter
the strings whose length is 0.
def check(x):
return True if len(x)>0 else False
list1 = ["ABC", "", "PQR", "XYZ", "", "IJK"]
list2 = list( filter(check, list1) )
print(list2)
* In above example, the strings with length=0 gets filtered.
* Refer following another example, where we will filter
the strings with length <3 and whose first and last
characters are unequal. In other words, we will collect
the Strings whose length is >=3 and first and last
characters are equal.
def check(x):
return True if len(x)>=3 and x[0]==x[-1] else False
list1 = ["1221", "ABC", "ABBAB", "XX", "100", "IJKI"]
list2 = list( filter(check, list1) )
print(list2)
* Here also, we can directly pass a lambda expression,
instead of defining and passing a separate function.
* For example:
---------------
list1 = ["1221", "ABC", "ABBAB", "XX", "100", "IJKI"]
list2 = list( filter(lambda x :True if len(x)>=3 and x[0]==x[-1] else False, list1)
)
print(list2)
The output will be:
--------------------
['1221', 'IJKI']
=====================================================
==== CRICKET GAME ====
import random
options = [0, 1, 1, 1, 2, 2, 2, 3, 4, 5, 6, "WIDE", "NO BALL", "OUT", "OUT", "OUT",
"OUT", "OUT"]
player = 1
while True:
print("Player", player, "press ENTER to play the BALL", end="")
input()
score = random.choice(options)
print(score)
if score=="OUT":
player = 2
================================================