Saturday, November 16, 2024
Google search engine
HomeLanguagesPython | Max/Min value in Nth Column in Matrix

Python | Max/Min value in Nth Column in Matrix

Sometimes, while working with Python Matrix, we may have a problem in which we require to find the minimum and maximum value 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 max()/min() + zip() 

This task can be solved using a combination of the above functions. In this, we pass in the zip() the list, to access all columns and max()/min() to get the maximum or minimum of columns.

  1. Firstly, a 2D list named test_list is initialized which contains three lists each containing three integers.
  2. The original list is printed using print() function and str() type conversion.
  3. A variable N is initialized with the value 2.
  4. zip() function is used to perform transpose operation on the given 2D list which results in the rows becoming columns and columns becoming rows. zip(*test_list) means that the asterisk(*) operator is used to unpack the list so that zip() can process each row as separate arguments.
    max() function is applied on each column to get the maximum value in the column.
  5. A list comprehension is used to store the maximum value of each column in a new list.
  6. The maximum value in the Nth column is accessed by indexing the new list with N.
  7. The result is printed using print() function and str() type conversion.

Python3




# Python3 code to demonstrate working of
# Max value in Nth Column in Matrix
# using max() + zip()
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Max value in Nth Column in Matrix
# using max() + zip()
res = [max(i) for i in zip(*test_list)][N]
 
# printing result
print("Max value of Nth column is : " + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7

Time complexity: O(n), where n is the total number of elements in the matrix. 
Auxiliary space: O(m), where m is the number of columns in the matrix. We create a temporary list of the maximum values in each column using a list comprehension. 

Method 2: Using lists and max() method

This task can be completed by storing all the elements in the last column in a new list and then finding the max() of all the elements from the newly created list to get our answer.

Python3




# Python3 code to demonstrate working of
# Max value in Nth Column in Matrix
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Max value in Nth Column in Matrix
x = []
for i in test_list:
    x.append(i[N])
res = max(x)
 
# printing result
print("Max value of Nth column is : " + str(res))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7

Time complexity: O(N), where N is the number of rows in the matrix. 

Auxiliary space complexity: O(N), where N is the number of rows in the matrix. 

Method 3: Using list comprehension and the built-in map() function 

In order to extract the Nth column then use the built-in max() function to find the maximum value.

Python3




# Python3 code to demonstrate working of
# Max value in Nth Column in Matrix
# using list comprehension and map()
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Max value in Nth Column in Matrix
# using list comprehension and map()
res = max(map(lambda x: x[N], test_list))
 
# printing result
print("Max value of Nth column is : " + str(res))
 
# This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using numpy library 

First convert the list test_list to a numpy array arr. Then we use the slicing operator [:, N] to extract the Nth column of the matrix and pass it to the np.max() function to find the maximum value in that column.

Python3




import numpy as np
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# convert list to numpy array
arr = np.array(test_list)
 
# find max value in Nth column
res = np.max(arr[:, N])
 
# printing result
print("Max value of Nth column is : " + str(res))


OUTPUT:

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7

The time complexity of the code after that depends on the size of the input matrix test_list. The time complexity of converting the list to numpy array using np.array() is O(N^2), where N is the size of the matrix. 

The space complexity of the code is O(N^2), since we are storing the entire matrix in the form of a numpy array.

Method 5: Using a for loop to iterate over the rows of the matrix and keep track of the maximum value in the Nth column.

Approach:

  1. Initialize a variable max_val to the minimum possible value.
  2. Iterate over the rows of the matrix using a for loop.
  3. For each row, extract the value in the Nth column using the indexing operator.
  4. If the extracted value is greater than max_val, update max_val to be equal to the extracted value.
  5. After iterating over all the rows, the value of max_val will be the maximum value in the Nth column.
  6. Print the result.

Python3




# Python3 code to demonstrate working of
# Max value in Nth Column in Matrix
# using for loop
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initialize N
N = 2
 
# Max value in Nth Column in Matrix
# using for loop
max_val = float('-inf')
for row in test_list:
    val = row[N]
    if val > max_val:
        max_val = val
 
# printing result
print("Max value of Nth column is : " + str(max_val))


Output

The original list is : [[5, 6, 7], [9, 10, 2], [10, 3, 4]]
Max value of Nth column is : 7

Time complexity: O(N), where N is the number of rows in the matrix.
Auxiliary space: O(1), as we only use a constant amount of extra memory to store the maximum value.

Method 6: Using the pandas library

Approach:

  1. Import the pandas library and create a DataFrame from the given list using the DataFrame() function.
  2. Select the Nth column of the DataFrame using the iloc[] function.
  3. Use the max() function to find the maximum value in the Nth column.

Python3




import pandas as pd
 
# initialize list
test_list = [[5, 6, 7],
             [9, 10, 2],
             [10, 3, 4]]
 
# convert list to DataFrame
df = pd.DataFrame(test_list)
 
# initialize N
N = 2
 
# find maximum value in Nth column
max_val = df.iloc[:, N].max()
 
# printing result
print("Max value of Nth column is : " + str(max_val))


Output:

Max value of Nth column is : 7

Time complexity: O(n), where n is the number of elements in the matrix.
Auxiliary space: O(n), as the matrix is stored in memory as a DataFrame.

RELATED ARTICLES

Most Popular

Recent Comments