Returns the weighted average of two or more numbers.
sum()
to sum the products of the numbers by their weight and to sum the weights.zip()
and a list comprehension to iterate over the pairs of values and weights.def weighted_average(nums, weights):
return sum(x * y for x, y in zip(nums, weights)) / sum(weights)
weighted_average([1, 2, 3], [0.6, 0.2, 0.3]) # 1.72727
Python, Math
Calculates the average of two or more numbers.
Python, Math
Initializes a list containing the numbers in the specified range where start
and end
are inclusive and the ratio between two terms is step
.
Python, Math
Converts a number to a list of digits.