Search code examples
pythonlistnumpyaverage

Calculating weighted average using two different list of lists


I have 2 list of lists as follows:

A = [[],[],[],[500,540],[510,560,550],[460,510,600,450]]

B = [[],[],[],[4000,2500],[3500,3600,3000],[3000,2900,3300,3500]]

I would like to calculate the weighted average of values in every list of A using lists of B as corresponding weights. My output C should like like:

C = [[],[],[],[515],[540],[505]]

What I tried so far is using

C = []
for i in range(len(A)):
        avg = np.average(A[i], weights=B[i]) 
        C.append(avg)

Many thanks for your help.


Solution

  • As others have pointed out, you need to deal with the empty lists. But no-one has given you the output you asked for. The other solutions also use counters (probably to mirror your own code), but this is not usually considered idiomatic Python.

    I would probably do this in a list comprehension, and instead of indexing into both lists I'd take advantage of zip().

    You said you wanted rounded integers in in 1-item lists, like:

     C = [[int(np.round(np.average(a, weights=b), 0))] if a else [] for a, b in zip(A, B)]
    

    This returns what you specified in your question:

    [[], [], [], [515], [540], [505]]
    

    But if it was me, I think I'd want the results as floats like:

    C = [np.average(a, weights=b) if a else np.nan for a, b in zip(A, B)]
    

    This results in:

    [nan, nan, nan, 515.3846153846154, 539.7029702970297, 505.03937007874015]
    

    which personally I would prefer.