SoFunction
Updated on 2024-10-30

Example of python to find weighted average (with pure python writeup)

First is the data source:

# List of data for which a weighted average is required
elements = []
# List of corresponding weights
weights = []

Solve directly using numpy:

import numpy as np
 
(elements, weights=weights)

With pure python writeup:

# Write without numpy 1
round(sum([elements[i]*weights[i] for i in range(n)])/sum(weights), 1)
 
# Write without numpy 2
round(sum([j[0]*j[1] for j in zip(elements, weights)])/sum(weights), 1)

The above example of this python for weighted average (with pure python writing method) is all that I have shared with you.