Python – Cost computation using Frequency and Price dictionary
Last Updated :
25 Mar, 2024
Given the the price and frequency dictionary, compute total cost of products, i.e. by summing the product of price and frequency of each item.
Input : test_dict = {"Apple" : 2, "Mango" : 2, "Grapes" : 2}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}
the put : 12
Explanation : (2*2) + (2*2) + (2*2) = 12.
Input : test_dict = {"Apple" : 3, "Mango" : 2, "Grapes" : 3}, {"Apple" : 2, "Mango" : 2, "Grapes" : 2}
Output : 16
Explanation : The summation of product leads to 16 as above.
Python – Cost computation using Frequency and Price dictionary
Below, are, examples of Python – Cost computation using the Frequency and Price dictionary.
Method #1: Using loop
This is the brute way in which this task can be performed. In this, we iterate through all the keys and multiply the frequency of each element by its cost, and keep performing intermediate summation.
Python3
# Python3 code to demonstrate working of
# Cost computation using Frequency and Price dictionary
# Using loop
# initializing dictionary
test_dict = {"Apple" : 5, "Mango" : 8, "Grapes" : 10}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing Frequency dict
cost_dict = {"Apple" : 3, "Mango" : 4, "Grapes" : 6}
res = 0
for key in test_dict:
# computing summation of product
res = res + (test_dict[key] * cost_dict[key])
# printing result
print("The extracted summation : " + str(res))
OutputThe original dictionary is : {'Apple': 5, 'Mango': 8, 'Grapes': 10}
The extracted summation : 107
Method #2 : Using sum() + list comprehension
The combination of these functionalities provide a shorthand to solve this problem. In this, we perform summation using sum() and list comprehension is used to compile result and iterate.
Python3
# Python3 code to demonstrate working of
# Cost computation using Frequency and Price dictionary
# Using sum() + list comprehension
# initializing dictionary
test_dict = {"Apple" : 5, "Mango" : 8, "Grapes" : 10}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# initializing Frequency dict
cost_dict = {"Apple" : 3, "Mango" : 4, "Grapes" : 6}
# using list comprehension and sum() to provide one-liner
res = sum([cost_dict[key] * test_dict[key] for key in test_dict])
# printing result
print("The extracted summation : " + str(res))
OutputThe original dictionary is : {'Apple': 5, 'Mango': 8, 'Grapes': 10}
The extracted summation : 107
Method #3: Using reduce+lambda
In this example, below The Python code multiplies corresponding values from two dictionaries (test_dict1 and test_dict2) and sums the results to compute the total cost. It then prints the original dictionary and the total cost.
Python3
# Python3 code to demonstrate working of
# Cost computation using Frequency and Price dictionary
# Using reduce() + map() + lambda
# importing reduce
from functools import reduce
# initializing dictionary
test_dict1 = {"Apple" : 5, "Mango" : 8, "Grapes" : 10}
# printing original dictionary
print("The original dictionary is : " + str(test_dict1))
# initializing Frequency dict
test_dict2 = {"Apple" : 3, "Mango" : 4, "Grapes" : 6}
# using reduce() + map() + lambda
result = reduce(lambda x, y: x + y, map(lambda x, y: x * y, test_dict1.values(), test_dict2.values()))
# printing result
print("The extracted summation : " + str(result))
OutputThe original dictionary is : {'Apple': 5, 'Mango': 8, 'Grapes': 10}
The extracted summation : 107
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...