Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Row Summation of Like Index Product

Python – Row Summation of Like Index Product

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




# 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))


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




# 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))


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




# 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 list comprehension
res = [sum([test_list[i][j] * mul_list[j] for j in range(len(mul_list))]) for i in range(len(test_list))]
 
# Printing result
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. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments