Open In App

Python – Column Maximum in Dictionary Value Matrix

Last Updated : 10 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a Dictionary with Matrix Values, compute maximum of each column of those Matrix.

Input  :  test_dict = {"Gfg" : [[7, 6], [3, 2]],
                                        "is" : [[3, 6], [6, 10]],
                                        "best" : [[5, 8], [2, 3]]}
Output : {'Gfg': [7, 6], 'is': [6, 10], 'best': [5, 8]}
Explanation :  7 > 3, 6 > 2, hence ordering.

Input  :  test_dict = {"Gfg" : [[7, 6], [3, 2]],
                                        "is" : [[3, 6], [6, 10]]}
Output : {'Gfg': [7, 6], 'is': [6, 10]}
Explanation :  6 > 3, 10 > 6, hence ordering.

Method #1 : Using dictionary comprehension + sorted() + items()

This is one of the ways in which this task can be performed. In this, The inner columns are extracted and sorted and last value of sorted list(maximum) is returned as result. This happens for all list values using dictionary comprehension.

Python3




# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using dictionary comprehension + sorted() + items()
 
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
            "is" : [[4, 6], [6, 8]],
            "best" : [[7, 4], [2, 3]]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# sorted() used to sort and "-1" used to get last i.e
# maximum element
res = {key : sorted(val, key = lambda ele : (ele[0], ele[1]))[-1] for key, val in test_dict.items()}
 
# printing result
print("The evaluated dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}

Time Complexity: O(n*nlogn), where n is the values in dictionary
Auxiliary Space: O(n), where n is the size of dictionary

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

This is one of the ways in which this task can be performed. In this, we extract maximum using max(), and align columns to list using zip() and map() is used to extend logic of zip to each column.

Python3




# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using max() + map() + zip()
 
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
            "is" : [[4, 6], [6, 8]],
            "best" : [[7, 4], [2, 3]]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# map extending logic to entire columns
# result compiled using dictionary comprehension
res = {key: list(map(max, zip(*val))) for key, val in test_dict.items()}
 
# printing result
print("The evaluated dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}

Method #3: Using list comprehension + map() + lambda function

Step-by-step approach:

  1. We initialize a dictionary called test_dict with three keys “Gfg”, “is”, and “best”. Each key has a value that is a list of lists representing a matrix with two rows and two columns.
  2. We print the original dictionary using the print() function and a string concatenation that includes the string “The original dictionary is : ” and the string representation of the test_dict dictionary using the str() function.
  3. We use a list comprehension with a map() function and a lambda function to find the maximum value of each column in each matrix of the test_dict dictionary. The map() function applies the lambda function to each tuple of values obtained by transposing the matrix using the zip() function. The list() function creates a list from the resulting map() object. Finally, the list comprehension creates a new dictionary with the same keys as test_dict but with the maximum values of each column as values.
  4. We print the resulting dictionary using the print() function and a string concatenation that includes the string “The evaluated dictionary : ” and the string representation of the res dictionary using the str() function.

Python3




# Python3 code to demonstrate working of
# Column Maximums of Dictionary Value Matrix
# Using list comprehension + map() + lambda function
 
# initializing dictionary
test_dict = {"Gfg" : [[5, 6], [3, 4]],
            "is" : [[4, 6], [6, 8]],
            "best" : [[7, 4], [2, 3]]}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# using list comprehension + map() + lambda function
res = {key: list(map(lambda x: max(x), zip(*val))) for key, val in test_dict.items()}
 
# printing result
print("The evaluated dictionary : " + str(res))


Output

The original dictionary is : {'Gfg': [[5, 6], [3, 4]], 'is': [[4, 6], [6, 8]], 'best': [[7, 4], [2, 3]]}
The evaluated dictionary : {'Gfg': [5, 6], 'is': [6, 8], 'best': [7, 4]}

Time complexity: O(n * m), where n is the number of keys in the dictionary, and m is the length of the value list for each key.
Auxiliary space: O(m), where m is the length of the value list for each key.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads