Python how to sum all the numbers in a dictionary
Question:
I’m new to python and having problems with summing up the numbers inside an element and then adding them together to get a total value.
Example of what I’m trying to do:
list = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
'area1': [395.0 * 212.0], 'area2': [165.0 * 110.0]
'area1': [83740], 'area2': [18150]
total value = 101890
Main.py:
def cubicMeterCalculator():
floorAreaList = {}
print("Example of how this would look like 'area1 395 212' 'area2 165 110'")
n = int(input("nHow many walls? "))
for i in range(n):
print("nEnter name of the wall first and 'Space' to separate the name and numbers before hitting enter.")
name, *lengths = input().split(" ")
l_lengths = list(map(float,lengths))
floorAreaList[name] = l_lengths
print(floorAreaList)
total = sum(float, floorAreaList)
print(total)
Answers:
You can use a generator expression to multiply the pairs of values in your dictionary, then sum
the output of that:
lst = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
total = sum(v[0]*v[1] for v in lst.values())
# 101890.0
You can find the area using list comprehension.
Iterate through lst.values()
-> dict_values([[395.0, 212.0], [165.0, 110.0]])
and multiply the elements. Finally, use sum
to find out the total.
lst = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
area = sum([i[0]*i[1] for i in lst.values()])
# 101890.0
As solution with map
+ sum
,
sum(map(lambda x: x[0]*x[1], lst.values()))
The answer with sum, map, and a lambda is probally the best for just areas.
Give this a try if there are more dimensions:
this approach scales with dimensions collected (areas and volumes). We can use the reduce function with a lambda expression to handle multidimensional situations (i.e. you collect LxWxH and want the sum of the volumes)
from functools import reduce
inputs = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
vals = [reduce(lambda x, y: x*y,p) for p in inputs.values()]
total = sum(vals)
print(total)
with more dimensions
inputs = {'vol1': [395.0, 212.0, 10.0], 'vol2': [165.0, 110.0, 10.0]}
vals = [reduce(lambda x, y: x*y,p) for p in inputs.values()]
total = sum(vals)
print(total)
I think something like this will do the trick
from functools import reduce
total = sum([reduce(lambda x, y: x*y, area) for area in floorAreaList.values()])
with values()
you get the values of the dictionary and then with the lambda
you multiply every element and in the end sum
them.
I’m new to python and having problems with summing up the numbers inside an element and then adding them together to get a total value.
Example of what I’m trying to do:
list = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
'area1': [395.0 * 212.0], 'area2': [165.0 * 110.0]
'area1': [83740], 'area2': [18150]
total value = 101890
Main.py:
def cubicMeterCalculator():
floorAreaList = {}
print("Example of how this would look like 'area1 395 212' 'area2 165 110'")
n = int(input("nHow many walls? "))
for i in range(n):
print("nEnter name of the wall first and 'Space' to separate the name and numbers before hitting enter.")
name, *lengths = input().split(" ")
l_lengths = list(map(float,lengths))
floorAreaList[name] = l_lengths
print(floorAreaList)
total = sum(float, floorAreaList)
print(total)
You can use a generator expression to multiply the pairs of values in your dictionary, then sum
the output of that:
lst = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
total = sum(v[0]*v[1] for v in lst.values())
# 101890.0
You can find the area using list comprehension.
Iterate through lst.values()
-> dict_values([[395.0, 212.0], [165.0, 110.0]])
and multiply the elements. Finally, use sum
to find out the total.
lst = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
area = sum([i[0]*i[1] for i in lst.values()])
# 101890.0
As solution with map
+ sum
,
sum(map(lambda x: x[0]*x[1], lst.values()))
The answer with sum, map, and a lambda is probally the best for just areas.
Give this a try if there are more dimensions:
this approach scales with dimensions collected (areas and volumes). We can use the reduce function with a lambda expression to handle multidimensional situations (i.e. you collect LxWxH and want the sum of the volumes)
from functools import reduce
inputs = {'area1': [395.0, 212.0], 'area2': [165.0, 110.0]}
vals = [reduce(lambda x, y: x*y,p) for p in inputs.values()]
total = sum(vals)
print(total)
with more dimensions
inputs = {'vol1': [395.0, 212.0, 10.0], 'vol2': [165.0, 110.0, 10.0]}
vals = [reduce(lambda x, y: x*y,p) for p in inputs.values()]
total = sum(vals)
print(total)
I think something like this will do the trick
from functools import reduce
total = sum([reduce(lambda x, y: x*y, area) for area in floorAreaList.values()])
with values()
you get the values of the dictionary and then with the lambda
you multiply every element and in the end sum
them.