Given a Matrix and elements list, For every row, extract the summation of product of elements with argument list.
Input : test_list = [[4, 5], [1, 5], [8, 2]], mul_list = [5, 2, 3]
Output : [30, 15, 44]
Explanation : For 1st row, (4*5) + (5*2) = 30, as value of 1st element of result list. This way each element is computed.Input : test_list = [[4, 5, 8, 2]], mul_list = [5, 2, 3, 9]
Output : [72]
Explanation : Similar computation as above method. Just 1 element as single Row.
Method #1 : Using loop
This is brute force way to solve this problem. In this, we perform iteration of each row and perform required summation in product using brute force approach using loop.
# Python3 code to demonstrate working of # Row Summation of Like Index Product # Using loop # initializing list test_list = [[ 3 , 4 , 5 ], [ 1 , 7 , 5 ], [ 8 , 1 , 2 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing mul list mul_list = [ 5 , 2 , 3 ] # Using loop res = [] for sub in test_list: sum = 0 for idx, ele in enumerate (sub): # performing summation of product with list elements sum = sum + (ele * mul_list[idx]) # adding each row sum res.append( sum ) # printing result print ( "List after multiplication : " + str (res)) |
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]] List after multiplication : [38, 34, 48]
Method #2 : Using map() + sum() + zip() + lambda
The combination of above functions can be used to solve this problem. In this, we perform the task of performing summation using sum(), zip() is used to map multiplication list with row values, and logic encapsulated and extended to each row, and its element using lambda and map().
# Python3 code to demonstrate working of # Row Summation of Like Index Product # Using map() + sum() + zip() + lambda # initializing list test_list = [[ 3 , 4 , 5 ], [ 1 , 7 , 5 ], [ 8 , 1 , 2 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing mul list mul_list = [ 5 , 2 , 3 ] # Using map() + sum() + zip() + lambda # Performing product in inner map, by zipping with elements list, and sum at outer map() used. res = list ( map ( sum , ( map ( lambda ele: ([x * y for x, y in zip ( ele, mul_list)]), test_list)))) # printing result print ( "List after multiplication : " + str (res)) |
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]] List after multiplication : [38, 34, 48]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.