Merge pull request #315 from casalazara/master

Create weighted_average.md
This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-09 09:12:57 +03:00
committed by GitHub

View File

@ -0,0 +1,18 @@
---
title: weighted_average
tags: math,list,intermediate
---
Returns the weighted average of two or more noumbers.
- Use `sum()` to sum the products of the numbers by their weight and to sum the weights.
- Use `zip()` and a list comprehension to iterate over the pairs of values and weights.
```py
def weighted_average(nums, weights):
return sum(x * y for x, y in zip(nums, weights)) / sum(weights)
```
```py
weighted_average([1, 2, 3], [0.6,0.2,0.3]) # 1.72727
```