Open In App

Python – Consecutive Row summation in Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the sum between the like indices when compared with the next list. The sum between the like elements in that index is returned. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using sum() + abs() + zip() + list comprehension This particular problem can also be solved using the combination of the above 4 operations. Here zip function does the dual task of pairing the list and also pairing the like indices for sum, to be computed by abs function and then sum is found using sum function, all bounded by list comprehension. 

Python3




# Python3 code to demonstrate
# Consecutive Row summation in Matrix
# using sum() + abs() + zip() + list comprehension
 
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using sum() + abs() + zip() + list comprehension
# Consecutive Row summation in Matrix
res = [sum(abs(i + j) for i, j in zip(*ele)) for ele in zip(test_list, test_list[1:])]
 
# print result
print("The row summation sublist : " + str(res))


Output : 

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The row summation sublist : [30, 30, 32]

Time Complexity: O(n*n),The above code iterates through the list once, hence the time complexity is quadratic, i.e. O(n*n).
Space Complexity: O(n),The algorithm uses an additional list to store the result, thus consuming linear space which is O(n).

Method #2 : Using sum() + map() + abs + zip() This task can also be achieved using the combination of above functions, the addition is map function that performs the task of binding of abs operation to the whole list. 

Python3




# Python3 code to demonstrate
# Consecutive Row summation in Matrix
# using sum() + map() + abs + zip()
 
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using sum() + map() + abs + zip()
# Consecutive Row summation in Matrix
res = [sum(map(abs, (i + j for i, j in zip(x, y)))) for x, y in zip(test_list, test_list[1:])]
 
# print result
print("The row summation sublist : " + str(res))


Output : 

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The row summation sublist : [30, 30, 32]

Time Complexity: O(N^2)
Auxiliary Space: O(N)

Method #3 : Using for loop + sum(),extend() methods

Python3




# Python3 code to demonstrate
# Consecutive Row summation in Matrix
 
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Consecutive Row summation in Matrix
res=[]
for i in range(0,len(test_list)):
    x=[]
    for j in range(0,len(test_list)):
        if(j-i==1):
            x.extend(test_list[i])
            x.extend(test_list[j])
            res.append(sum(x))
# print result
print("The row summation sublist : " + str(res))


Output

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The row summation sublist : [30, 30, 32]

Time Complexity : O(N*N)
Auxiliary Space : O(N)

Method #4: Using numpy library

Note: Install numpy module using command “pip install numpy”

Python3




import numpy as np
 
# initializing list
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
 
# printing original list
print("The original list : " + str(test_list))
 
# Convert to numpy array
test_matrix = np.array(test_list)
res = [np.sum(np.abs(test_matrix[i] + test_matrix[i + 1])) for i in range(len(test_matrix) - 1)]
 
# print result
print("The row summation sublist : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output:

The original list : [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
The row summation sublist : [30, 30, 32]

Time Complexity: O(n*m), where n is the number of rows and m is the number of columns in the matrix. 
Auxiliary Space: O(n) for storing the result list.

Method #7: Using itertools module

We can use the itertools module to generate the consecutive pairs of rows and then use a list comprehension to calculate the row summations.

Step-by-step approach:

  • Import the itertools module.
  • Use the itertools.combinations() function to generate all possible pairs of rows from the original list.
  • Next, use a list comprehension to filter out the pairs that are not consecutive, and calculate the row summation for each consecutive pair using the sum() function and the itertools.chain() function to flatten the list.
  • Finally, print the row summations.

Below is the implementation of the above approach:

Python3




import itertools
 
test_list = [[3, 4, 5], [4, 6, 8], [1, 9, 2], [3, 7, 10]]
 
sums = [sum(list(itertools.chain(*pair))) for pair in itertools.combinations(test_list, 2) if abs(test_list.index(pair[0]) - test_list.index(pair[1])) == 1]
 
print("The row summation sublist : " + str(sums))


Output

The row summation sublist : [30, 30, 32]

The time complexity of the code is O(n^2), where n is the number of rows in the input matrix. 
The space complexity of the code is O(n^2), because we are generating all possible pairs of rows, which takes up O(n^2) space in memory.



Last Updated : 28 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads