Python - Row with Minimum Sum in Matrix
Last Updated :
17 Apr, 2023
We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but having shorthands to perform the same are always helpful as this kind of problem can come in web development.
Method #1 : Using reduce() + lambda The above two function can help us achieving this particular task. The lambda function does the task of logic and iteration and reduce function does the task of returning the required result. Works in python 2 only.
Python3
# Python code to demonstrate
# Minimum Sum row in Matrix
# using reduce() + lambda
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
# using reduce() + lambda
# Minimum Sum row in Matrix
res = reduce(lambda i, j: i if sum(i) < sum(j) else j, test_matrix)
# printing result
print("Minimum sum row is : " + str(res))
Output : The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Minimum sum row is : [1, 3, 1]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Method #2: Using min() + key The min function can get the minimum of all the list and key is used to specify on what the min condition has to be applied which is summation in this case.
Python3
# Python3 code to demonstrate
# Minimum Sum row in Matrix
# using min() + key
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
# printing the original matrix
print("The original matrix is : " + str(test_matrix))
# using min() + key
# Minimum Sum row in Matrix
res = min(test_matrix, key=sum)
# printing result
print("Minimum sum row is : " + str(res))
Output : The original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Minimum sum row is : [1, 3, 1]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Method #3 : Using sum(),extend(),sort() and index() methods
Python3
# Python code to demonstrate
# Minimum Sum row in Matrix
# initializing matrix
test_matrix = [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
# printing the original matrix
print ("The original matrix is : " + str(test_matrix))
a=[]
for i in test_matrix:
a.append(sum(i))
y=[]
y.extend(a)
y.sort()
res=test_matrix[a.index(y[0])]
# printing result
print ("Minimum sum row is : " + str(res))
OutputThe original matrix is : [[1, 3, 1], [4, 5, 3], [1, 2, 4]]
Minimum sum row is : [1, 3, 1]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a dictionary with m * n keys and a list of m * n elements
Method #4: Using numpy library
Note: Install numpy module using command "pip install numpy"
Python3
# Python code to demonstrate
# Minimum Sum row in Matrix
# using numpy
import numpy as np
# initializing matrix
test_matrix = np.array([[1, 3, 1], [4, 5, 3], [1, 2, 4]])
# printing the original matrix
print("The original matrix is : \n", test_matrix)
# using numpy
# Minimum Sum row in Matrix
res = test_matrix[np.argmin(np.sum(test_matrix, axis=1))]
# printing result
print("Minimum sum row is : ", res)
Note: Install numpy module using command "pip install numpy"
Output:
The original matrix is :
[[1 3 1]
[4 5 3]
[1 2 4]]
Minimum sum row is : [1 3 1]
The above code uses the numpy library to find the row with the minimum sum in the matrix. The argmin() function is used to find the index of the row with the minimum sum and the sum() function is used to calculate the sum of elements in each row. The axis parameter is set to 1 to calculate the sum of elements in each row. The result is then stored in the variable 'res'. The time complexity is O(n) and Auxiliary space is O(1)
Similar Reads
Python | Row with Minimum element in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Python - Row with Minimum difference in extreme values Given a Matrix, extract the rows with a minimum difference in extreme values. Examples: Input : test_list = [[4, 10, 18], [5, 0], [1, 4, 6], [19, 2]] Output : [[1, 4, 6], [5, 0]] Explanation : 6 - 1 = 5, 5 - 0 = 5, is minimum difference between extreme values. Input : test_list = [[4, 10, 18], [5, 0
4 min read
Python - Uneven Sized Matrix Column Minimum The usual list of list, unlike conventional C type Matrix, can allow the nested list of lists with variable lengths, and when we require the minimum of its columns, the uneven length of rows may lead to some elements in that elements to be absent and if not handled correctly, may throw exception. Le
7 min read
Python Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9Output : Median is 5If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9)Input: 1 3 4 2 5 6 7 8 9Output: Median is
5 min read
Python - Find Minimum Pair Sum in list Sometimes, we need to find the specific problem of getting the pair which yields the minimum sum, this can be computed by getting initial two elements after sorting. But in some case, we donât with to change the ordering of list and perform some operation in the similar list without using extra spac
4 min read
Python | Row lengths in Matrix The problems concerning matrix are quite common in both competitive programming and Data Science domain. One such problem that we might face is of finding the lengths of rows of matrix in uneven sized matrix. Let's discuss certain ways in which this problem can be solved. Method #1 : Using max() + m
4 min read
Python - Minimum in each record value list Many times, while dealing with containers in any language we come across lists of tuples in different forms, tuples in themselves can have sometimes more than native datatypes and can have list as their attributes. This article talks about the min of list as tuple attribute. Letâs discuss certain wa
6 min read
Python - Sort Matrix by K Sized Subarray Maximum Sum Given Matrix, write a Python program to sort rows by maximum of K sized subarray sum. Examples: Input : test_list = [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [4, 3, 9, 3, 9], [5, 4, 3, 2, 1]], K = 3 Output : [[4, 3, 5, 2, 3], [6, 4, 2, 1, 1], [5, 4, 3, 2, 1], [4, 3, 9, 3, 9]] Explanation : 12 = 12 = 12
4 min read
Python | Minimum Difference in Matrix Columns This particular article focuses on a problem that has utility in competitive as well as day-day programming. Sometimes, we need to get the minimum difference between the like indices when compared with the next list. The minimum difference between the like elements in that index is returned. Letâs d
3 min read