Sometimes, while working with Python dictionaries, we can have problem in which we need to convert the values into proportions with respect to total. This can have applications in Data Science and Machine Learning domain. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using sum() + loop The combination of above functionalities can be used to solve this problem. In this, we perform the task of finding sum using sum(). And the task of division is done inside a loop using division with sum of each value.
Python3
# Python3 code to demonstrate working of # Convert Values into proportions # Using sum() + loop # initializing dictionary test_dict = { 'gfg' : 10 , 'is' : 15 , 'best' : 20 } # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Convert Values into proportions # Using sum() + loop temp = sum (test_dict.values()) for key, val in test_dict.items(): test_dict[key] = val / temp # printing result print ("The proportions divided values : " + str (test_dict)) |
The original dictionary is : {‘is’: 15, ‘best’: 20, ‘gfg’: 10} The proportions divided values : {‘is’: 0.3333333333333333, ‘best’: 0.4444444444444444, ‘gfg’: 0.2222222222222222}
Time complexity: O(n), where n is the number of values in the dictionary.
Auxiliary Space: O(1), constant extra space is required
Method #2 : Using dictionary comprehension + sum() The combination of above functions can be used to perform this task. In this, we calculate sum in similar manner as above method, and dictionary comprehension is used to perform the task of looping in one liner.
Python3
# Python3 code to demonstrate working of # Convert Values into proportions # Using dictionary comprehension + sum() # initializing dictionary test_dict = { 'gfg' : 10 , 'is' : 15 , 'best' : 20 } # printing original dictionary print ("The original dictionary is : " + str (test_dict)) # Convert Values into proportions # Using dictionary comprehension + sum() temp = sum (test_dict.values()) res = {key: val / temp for key, val in test_dict.items()} # printing result print ("The proportions divided values : " + str (res)) |
The original dictionary is : {‘is’: 15, ‘best’: 20, ‘gfg’: 10} The proportions divided values : {‘is’: 0.3333333333333333, ‘best’: 0.4444444444444444, ‘gfg’: 0.2222222222222222}
Method #3 : Using map()+lambda()
Approach
the map() function to apply a lambda function that divides each value in the dictionary by the total sum, and then returns a tuple with the same key and the new proportion value. The resulting tuples are then converted to a dictionary using the dict() function.
Algorithm
1. Define a dictionary d with keys and values
2. Compute the total sum of values in the dictionary d using the sum() function and assign it to the variable total_sum
3. Use the map() function to apply a lambda function that takes each key-value pair in d, divides the value by the total_sum, and returns a tuple with the same key and the new proportion value
4. Convert the resulting tuples to a dictionary using the dict() function and assign it to the variable proportions
5. Print the dictionary proportions that contains the original keys as well as their corresponding proportion values.
Python3
d = { 'is' : 15 , 'best' : 20 , 'gfg' : 10 } #give input total_sum = sum (d.values()) #usingvalues to find the total sum proportions = dict ( map ( lambda key_value: (key_value[ 0 ], key_value[ 1 ] / total_sum), d.items())) #using map and lambda functions to find the proportions print (proportions) #print the output |
{'is': 0.3333333333333333, 'best': 0.4444444444444444, 'gfg': 0.2222222222222222}
Time complexity: O(n), Computing the total sum of values in the dictionary d using the sum() function takes O(n) time, where n is the number of elements in the dictionary. Applying the map() function and the lambda function on the dictionary d takes O(n) time since we are iterating over all the elements in the dictionary once. Converting the resulting tuples to a dictionary using the dict() function also takes O(n) time since we are iterating over all the tuples in the list once. Therefore, the total time complexity is O(n).
Auxiliary Space: O(n), we are creating a new dictionary proportions that has the same number of elements as the original dictionary d. We are also creating a few temporary variables to hold the total sum of values, the tuples returned by the lambda function, and the list of tuples before converting them to a dictionary. However, the space used by these temporary variables is negligible compared to the size of the dictionaries.
Method #4: Using the reduce() function and a lambda function:
1.Import the reduce function from the functools module.
2.Create a dictionary named test_dict with key-value pairs.
3.Print the original dictionary.
4.Use the reduce function to get the sum of all values in the dictionary.
The reduce function takes two arguments, a lambda function and an iterable. In this case, the lambda function takes two arguments x and y, and returns their sum. The iterable is the values of the test_dict dictionary, obtained using the values() method.
5.Use a dictionary comprehension to divide each value in the dictionary by the sum of all values.
The dictionary comprehension creates a new dictionary res, where each key-value pair is obtained by iterating over the items of the test_dict dictionary. For each item, the key is k and the value is v / temp, where v is the value of the item and temp is the sum of all values in the dictionary.
6.Print the resultant dictionary.
Python3
# Importing reduce from functools module from functools import reduce # Initializing the dictionary test_dict = { 'gfg' : 10 , 'is' : 15 , 'best' : 20 } # Printing the original dictionary print ( "The original dictionary is : " + str (test_dict)) # Using reduce to get the sum of all values in the dictionary temp = reduce ( lambda x, y: x + y, test_dict.values()) # Using dictionary comprehension to divide each value by the sum of all values res = {k: v / temp for k, v in test_dict.items()} # Printing the resultant dictionary print ( "The proportions divided values : " + str (res)) #This code is contributed by Jyothi pinjala |
The original dictionary is : {'gfg': 10, 'is': 15, 'best': 20} The proportions divided values : {'gfg': 0.2222222222222222, 'is': 0.3333333333333333, 'best': 0.4444444444444444}
The time complexity : O(n), where n is the number of key-value pairs in the dictionary. This is because the code needs to iterate over all the values in the dictionary to calculate the sum of values, and then iterate over all the key-value pairs again to calculate the proportions.
The auxiliary space :O(n) as well, because the code creates a new dictionary with the same number of key-value pairs as the original dictionary. However, the space complexity can be considered as O(2n) which simplifies to O(n) because the original dictionary and the new dictionary have the same size.