![]() |
VOOZH | about |
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 : Using sum() + zip() This task can be solved using the combination of above functions. In this, we pass in the zip() the list, to access all columns and sum() to get summation of columns.
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
Time Complexity: O(n) where n is the number of elements in the string list. The sum() + zip() is used to perform the task and it takes O(n) time.
Auxiliary Space: O(1) constant additional space is created.
Method #2 : Using loop This is brute force way to solve this problem. In this, we iterate through the matrix and take summation of column by testing column number.
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
Time Complexity: O(n), where n is the total number of elements in the matrix.
Auxiliary Space: O(1). This is because the code uses a constant amount of extra space to store the sum of the Kth column, regardless of the size of the input matrix.
Method #3 : Using numpy
Note: Install numpy module using command "pip install numpy"
Another approach to find the summation of the kth column in a matrix is to use the numpy library in Python. The numpy library provides a variety of mathematical functions and operations that can be applied to arrays and matrices.
One way to find the summation of the kth column in a matrix is to use the numpy function sum() along with the axis argument. By setting the axis argument to 0, we can specify that we want to sum the values along the columns, and by specifying the kth column index, we can find the summation of that specific column.
Output:
Sum of Kth column is : 13
Time Complexity: O(n)
Auxiliary Space: O(1) where n is the number of elements in matrix.
Method 4: Using the built-in function map() along with the sum() function
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
The time complexity of this approach is O(n), where n is the total number of elements in the input list.
The space complexity is O(n), because we create a new list kth_col to store the extracted Kth elements from each sub-list of test_list.
Method 5: Using reduce() function from functools module
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
Time complexity: O(n), where n is the number of elements in the test_list.
Auxiliary space: O(n), where n is the number of elements in the test_list.
Method #6: Using list comprehension and sum() function
Step-by-step approach:
Below is the implementation of the above approach:
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
Time complexity: O(N), where N is the total number of elements in the 'test_list'.
Auxiliary space: O(N), to store the 'kth_col' list.
Method #7: Using heapq:
Algorithm:
The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]] Sum of Kth column is : 13
Time Complexity: O(n log k), where n is the number of elements in the input list and k is the value of K.
Space Complexity: O(k), where k is the value of K.