Files
30-seconds-of-code/snippets/weighted_average.md
Tim Gates 3494adc4b3 docs: fix simple typo, noumbers -> numbers
There is a small typo in snippets/weighted_average.md.

Should read `numbers` rather than `noumbers`.
2020-12-25 00:00:53 +11:00

19 lines
473 B
Markdown

---
title: weighted_average
tags: math,list,intermediate
---
Returns the weighted average of two or more numbers.
- 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
```