Sometimes, while working with records, we can have a problem in which we need to the maximum all the columns of a container of lists that are tuples. This kind of application is common in the web development domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using max() + list comprehension + zip()
This task can be performed using a combination of above functions. In this, we cumulate the like index elements, i.e columns using zip(), and then iterate through them using list comprehension and perform maximum using max().
Python3
# Python3 code to demonstrate working of # Records Maxima in List of Tuples # using list comprehension + max() + zip() # initialize list test_list = [( 1 , 2 , 3 ), ( 6 , 7 , 6 ), ( 1 , 6 , 8 )] # printing original list print ( "The original list : " + str (test_list)) # Records Maxima in List of Tuples # using list comprehension + max() + zip() res = [ max (ele) for ele in zip ( * test_list)] # printing result print ( "The Cumulative column maximum is : " + str (res)) |
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the length of the longest tuple in the list.
Method #2 : Using zip() + map() + max()
This method is similar to the above method. In this, the task performed by list comprehension is performed by map(), which extends the maximum of columns to zipped elements.
Python3
# Python3 code to demonstrate working of # Records Maxima in List of Tuples # using zip() + map() + max() # initialize list test_list = [( 1 , 2 , 3 ), ( 6 , 7 , 6 ), ( 1 , 6 , 8 )] # printing original list print ( "The original list : " + str (test_list)) # Records Maxima in List of Tuples # using zip() + map() + max() res = list ( map ( max , zip ( * test_list))) # printing result print ( "The Cumulative column maximum is : " + str (res)) |
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n), where n is the number of tuples in the list.
Auxiliary space: O(m), where m is the length of the longest tuple in the list.
Method#3: Using Recursive method.
Python3
def find_maxima_recursive(lst, index, result): if not lst: return result else : result[index] = max (result[index], lst[ 0 ][index]) return find_maxima_recursive(lst[ 1 :], index, result) def find_maxima(lst): if not lst: return [] result = [ 0 ] * len (lst[ 0 ]) for i in range ( len (lst[ 0 ])): find_maxima_recursive(lst, i, result) return result # initialize list test_list = [( 1 , 2 , 3 ), ( 6 , 7 , 6 ), ( 1 , 6 , 8 )] # printing original list print ( "The original list : " + str (test_list)) res = find_maxima(test_list) # printing result print ( "The Cumulative column maximum is : " + str (res)) #this code contributed by tvsk |
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]
Time Complexity: O(n*m)
Auxiliary Space: O(n)
Method#4: using enumerate() + max() + zip()
Algorithm:
- Initialize an empty list “res”.
- Loop through each tuple in the list “test_list”.
- If it is the first tuple, add it to the “res” list as is.
- If it is not the first tuple, compare each element in the current tuple with its corresponding element in the “res” list and keep the maximum value.
- Update the “res” list with the maximum values.
- Return the “res” list.
Python3
# Python3 code to demonstrate working of # Records Maxima in List of Tuples # using enumerate() + max() + zip() # initialize list test_list = [( 1 , 2 , 3 ), ( 6 , 7 , 6 ), ( 1 , 6 , 8 )] # printing original list print ( "The original list : " + str (test_list)) # Records Maxima in List of Tuples # using enumerate() + max() + zip() res = [] for i, tup in enumerate (test_list): if i = = 0 : res = list (tup) else : res = [ max (x, y) for x, y in zip (res, tup)] # printing result print ( "The Cumulative column maximum is : " + str (res)) #This code is contributed by Vinay Pinjala. |
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)] The Cumulative column maximum is : [6, 7, 8]
Time complexity: O(n*m), where n is the number of tuples in the list and m is the number of elements in each tuple. The loop runs n times, and the max() function runs m times for each iteration of the loop.
Auxiliary Space: O(m), where m is the number of elements in each tuple. The “res” list stores the maximum values for each element, so its length is equal to the number of elements in each tuple..
Method#5: Using numpy:
Algorithm:
1.Import the required library numpy.
2.Initialize the list of tuples.
3.Use the numpy library’s max function and pass the list of tuples as a parameter, and set the axis as 0. This will find the maximum value of each 4.element in each tuple for all the tuples.
5.Convert the numpy array to a list and print the result.
Python3
import numpy as np # initialize list test_list = [( 1 , 2 , 3 ), ( 6 , 7 , 6 ), ( 1 , 6 , 8 )] # printing original list print ( "The original list : " + str (test_list)) # finding maximum of each column using numpy's max() function and setting axis=0 to get column-wise maximums max_list = np. max (test_list, axis = 0 ) # printing the resulting list as a Python list using tolist() method print ( "The Cumulative column maximum is:" , max_list.tolist()) #This code is contributed by Jyothi pinjala. |
Output:
The original list : [(1, 2, 3), (6, 7, 6), (1, 6, 8)]
The Cumulative column maximum is: [6, 7, 8]
Time Complexity: O(N*M), where N is the number of tuples and M is the number of elements in each tuple.
Space Complexity: O(M), where M is the number of elements in each tuple.