Open In App

Python | Maximize Column in Records List

Sometimes, we encounter a problem where we deal with a complex type of matrix column maximization in which we are given a tuple and we need to perform the maximization of its like elements. This has a good application in the Machine Learning domain. Let’s discuss certain ways in which this can be done.

Method #1 : Using zip() + list comprehension



This problem can be resolved using the list comprehension which could perform the column maximization logic and zip function is used to bind the elements as a result and also at the time of vertical maximization. 




# Python3 code to demonstrate
# Maximize Column in Records List
# using list comprehension + zip()
 
# initializing list
test_list = [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
 
# printing original list
print("The original list : " + str(test_list))
 
# using list comprehension + zip()
# Maximize Column in Records List
res = [tuple(max(j) for j in zip(*i)) for i in zip(*test_list)]
 
# print result
print("The maximization of columns of tuple list : " + str(res))

Output : 

The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
The maximization of columns of tuple list : [(3, 7), (2, 9), (10, 5)]

Time complexity: O(n*m), where n is the length of the outer list test_list, and m is the maximum length of the inner lists.
Auxiliary space: O(n*m), as we are creating a new list res of the same size as the input list test_list. 

Method #2: Using zip() + map()

The task of binding the column elements can also be performed using the map function and the zip function performs the task of binding the maximized tuples. Both logics are bound by list comprehension. 




# Python3 code to demonstrate
# Maximize Column in Records List
# using zip() + map()
 
# Initializing list
test_list = [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Maximize Column in Records List
# using zip() + map() function
res = [tuple(map(max, zip(*i))) for i in zip(*test_list)]
 
# Printing result
print("The maximization of columns of tuple list : " + str(res))

Output : 
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
The maximization of columns of tuple list : [(3, 7), (2, 9), (10, 5)]

Time complexity: O(n*m), where n is the length of the outer list test_list, and m is the maximum length of the inner lists.
Auxiliary space: O(n*m), as we are creating a new list res of the same size as the input list test_list. 

Method #4: Using numpy library




import numpy as np
 
# Input list
test_list = [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
 
 
arr = np.array(test_list)
res = tuple(np.max(arr, axis=0))
 
# Printing the resultant tuple list
print("The maximization of columns of tuple list : " + str(res))

OUTPUT:
The maximization of columns of tuple list : (array([3, 7]), array([2, 9]), array([10,  5]))

Time complexity: O(N), where N is the total number of elements in the input list. 
Auxiliary space: O(N), where N is the total number of elements in the input list. 

Method 4: Using nested loops




# Python3 code to demonstrate
# Maximize Column in Records List
# using nested loops
 
# Initializing list
test_list = [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
 
# Printing original list
print("The original list : " + str(test_list))
 
# Using nested loops
# Maximize Column in Records List
res = []
 
for i in range(len(test_list[0])):
    temp = []
    for j in range(len(test_list)):
        temp.append(test_list[j][i])
    res.append(tuple(max(x) for x in zip(*temp)))
 
# Prining the resultan tuple list
print("The maximization of columns of tuple list : " + str(res))

Output
The original list : [[(1, 4), (2, 3), (5, 2)], [(3, 7), (1, 9), (10, 5)]]
The maximization of columns of tuple list : [(3, 7), (2, 9), (10, 5)]

Time complexity: O(n^2) where n is the length of the longest inner list
Auxiliary space: O(n) where n is the length of the longest inner list


Article Tags :