Efficient way to sum a product of numbers...

Paul Rubin http
Mon Aug 31 20:27:03 CEST 2009


vsoler <vicente.soler at gmail.com> writes:
>          m= [[ 'a', 1], [ 'b', 2],[ 'a', 3]]
>          r={'a':4, 'b':5, 'c':6}
> 
> What I need is the calculation
> 
>          1*4 + 2*5 + 3*4 = 4 + 10 + 12 = 26

sum(r[k]*w for k,w in m)



More information about the Python-list mailing list