Weighted Mean

Given a discrete set of numbers, XX, and a corresponding set of weights, WW, the weighted mean is calculated as follows: mw=i=1n(xiwi)i=1nwim_w = \frac{\sum_{i=1}^n (x_i * w_i)}{\sum_{i=1}^n w_i}, where xix_i and wiw_i are the respective ithi^{th} corresponding elements of XX and WW.

For example, if X={1,3,5}X = \{1, 3, 5\}and W={2,4,6}W=\{2, 4, 6\}, our weighted mean would be:

mw=(12)+(34)+(56)2+4+6=2+12+3012=3.66ˉm_w = \frac{(1*2)+(3*4)+(5*6)}{2 + 4 + 6}=\frac{2+12+30}{12}=3.\bar{66}

If we wanted to round this to a scale of decimal place, our result would be .

num = int(input())
values = list(map(int, input().split()))
weights = list(map(int, input().split()))

v = list(map(lambda x, y: x*y, values, weights))
wmean= sum(v)/sum(weights)

print('%.1f' % wmean)

Last updated