λ +
Python compact coding is very much possible with lambda function.
Anonymous function.. Function without name.
# lambda arguments : expression
# using Lambda
result = lambda x,y : x + y
# print(result(5,5))
# otherwise by function
def add(x,y):
return (x+y)
add(5,5)
sorted() takes elements from the list and sort the elements. Please see how key is implemented using lambda function. This can be used for any list with numbers, strings, list of lists. For simple understanding, number list is given. keys are defined using lambda arguments.
# Difference Between function and lambda -anonymous function
def cube(x):
return (x*x*x)
lambda_cube = lambda x: x*x*x
print(cube(125))
print((lambda_cube(25))) # No function call or return
list_of_tuples = [(1, 2, 3, 4), (4, -3, 2, 1), (3, 4, -1, 2), (2, 3, 4, -1)]
list_of_tuples_sorted = sorted(list_of_tuples)
print(list_of_tuples_sorted)
"""
[(1, 2, 3, 4), (2, 3, 4, -1), (3, 4, -1, 2), (4, -3, 2, 1)]
"""
list_of_tuples_sorted = sorted(list_of_tuples, key =lambda x: x[1]) # Sort in second element of all tuples
print(list_of_tuples_sorted)
"""
[(4, -3, 2, 1), (1, 2, 3, 4), (2, 3, 4, -1), (3, 4, -1, 2)]
"""
list_of_tuples_sorted = sorted(list_of_tuples, key =lambda x: x[3]) # Sort in last element of all tuples
print(list_of_tuples_sorted)
"""
[(2, 3, 4, -1), (4, -3, 2, 1), (3, 4, -1, 2), (1, 2, 3, 4)] """
Map function is used to pass the arguments from the list. Literally mapping values..
# Using function map()
alist = [1, 2, 3, 4, 5]
mlist = map(lambda x : x*x, alist) # map supplies elements instead of one element
print(list(mlist)) # [1, 4, 9, 16, 25]
# Otherwise using c-omprehensive list
clist = [x * x for x in alist]
print(clist) # [1, 4, 9, 16, 25]
#Using Filter to find even numbers in the list
flist = filter(lambda x: x%2 == 0, alist)
print(list(flist)) # [2, 4]
# Otherwise using c-omprehensive list
clist = [x for x in alist if x%2== 0]
print(clist) # [2, 4]
#Using reduce ....please import reduce from functools
def my_add(a, b):
result = a + b
print(f"{a} + {b} = {result}")
return result
from functools import reduce
numbers = [0, 1, 2, 3, 4]
print(reduce(my_add, numbers))
# Note passing a,b and see the cumulative sum is first
element in the next iteration
0 + 1 = 1
1 + 2 = 3
3 + 3 = 6
6 + 4 = 10
10
"""
import functools
# list definition
alist = [1, 2, 7, 4, 5, 6, 3, ]
# educe to compute sum of list
print("The sum of the list elements is : ", end="")
print(functools.reduce(lambda a, b: a + b, alist)) #28
# reduce to compute maximum element from list
print("The maximum element of the list is : ", end="")
print(functools.reduce(lambda a, b: a if a > b else b, alist)) # 7
To understand the visualization the above reduce code execution: please click the link https://pythontutor.com/visualize.html#mode=display Paste the above code in the editor window. Click visualize execution and next button.
Step 1:
In this screen shot, please notice lambda list with a =1 ,b=2 . Press Clicks to see the screen below to see the return value 3:
Happy Learning with AMET, ODL
No comments:
Post a Comment