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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


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)]

 

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 bound by list comprehension.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))
  
# using zip() + map()
# Maximize Column in Records List
res = [tuple(map(max, zip(*i))) for i in zip(*test_list)]
  
# print result
print("The maximization of columns of tuple list : " + str(res))

chevron_right


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)]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.