Python – Row Summation of Like Index Product
Last Updated :
02 May, 2023
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
test_list = [[ 3 , 4 , 5 ], [ 1 , 7 , 5 ], [ 8 , 1 , 2 ]]
print ("The original list is : " + str (test_list))
mul_list = [ 5 , 2 , 3 ]
res = []
for sub in test_list:
sum = 0
for idx, ele in enumerate (sub):
sum = sum + (ele * mul_list[idx])
res.append( sum )
print (" List after multiplication : " + str (res))
|
Output :
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
List after multiplication : [38, 34, 48]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n), where n is the number of rows in the matrix, for storing the row sums in the result list.
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
test_list = [[ 3 , 4 , 5 ], [ 1 , 7 , 5 ], [ 8 , 1 , 2 ]]
print ("The original list is : " + str (test_list))
mul_list = [ 5 , 2 , 3 ]
res = list ( map ( sum , ( map ( lambda ele: ([x * y for x, y in zip (
ele, mul_list)]), test_list))))
print (" List after multiplication : " + str (res))
|
Output :
The original list is : [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
List after multiplication : [38, 34, 48]
Time complexity: O(nm), where n is the number of rows and m is the number of columns in the matrix.
Auxiliary Space: O(n), where n is the number of rows in the matrix, for storing the row sums in the result list.
Method #3: Using list comprehension
Python3
test_list = [[ 3 , 4 , 5 ], [ 1 , 7 , 5 ], [ 8 , 1 , 2 ]]
print ( "The original list is: " + str (test_list))
mul_list = [ 5 , 2 , 3 ]
res = [ sum ([test_list[i][j] * mul_list[j] for j in range ( len (mul_list))]) for i in range ( len (test_list))]
print ( "List after multiplication: " + str (res))
|
Output
The original list is: [[3, 4, 5], [1, 7, 5], [8, 1, 2]]
List after multiplication: [38, 34, 48]
Time complexity: O(n^2), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...