Python - Consecutive Row summation in Matrix
Last Updated :
28 Mar, 2023
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))
OutputThe 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))
OutputThe 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.
Similar Reads
Summation Matrix columns - Python The task of summing the columns of a matrix in Python involves calculating the sum of each column in a 2D list or array. For example, given the matrix a = [[3, 7, 6], [1, 3, 5], [9, 3, 2]], the goal is to compute the sum of each column, resulting in [13, 13, 13]. Using numpy.sum()numpy.sum() is a hi
2 min read
Python - Test Consecutive Element Matrix Given a Matrix, test if it is made of consecutive elements. Input : test_list = [[4, 5, 6], [7], [8, 9, 10], [11, 12]] Output : True Explanation : Elements in Matrix range from 4 to 12. Input : test_list = [[4, 5, 6], [7], [8, 18, 10], [11, 12]] Output : False Explanation : Elements not consecutive
6 min read
Python - Summation of kth column in a matrix Sometimes, while working with Python Matrix, we may have a problem in which we require to find the summation of a particular column. This can have a possible application in day-day programming and competitive programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Us
8 min read
Python - Tuple Matrix Columns Summation Sometimes, while working with Tuple Matrix, we can have a problem in which we need to perform summation of each column of tuple matrix, at the element level. This kind of problem can have application in Data Science domains. Let's discuss certain ways in which this task can be performed. Input : tes
8 min read
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 e
4 min read
Python Program to Sort Matrix Rows by summation of consecutive difference of elements Given a Matrix, the following article depicts how to sort rows of a matrix on the basis of summation of difference between consecutive elements of a row. Input : test_list = [[1, 5, 3, 6], [4, 3, 2, 1], [7, 2, 4, 5], [6, 9, 3, 2]], Output : [[4, 3, 2, 1], [7, 2, 4, 5], [1, 5, 3, 6], [6, 9, 3, 2]] Ex
7 min read