遇到一个很难看的代码
val = reduce(lambda a, b: a+b, map(lambda t: t[0]*t[1], zip([10, 20, 30], [0.1, 0.3, 0.7])), 0.0)
print(val)
28.0
print(list(zip([10, 20, 30], [0.1, 0.3, 0.7])))
[(10, 0.1), (20, 0.3), (30, 0.7)]
print(list(map(lambda t: t[0]*t[1], zip([10, 20, 30], [0.1, 0.3, 0.7]))))
[1.0, 6.0, 21.0]
print(reduce(lambda a, b: a+b, map(lambda t: t[0]*t[1], zip([10, 20, 30], [0.1, 0.3, 0.7])),0.0))
28.0
import numpy as np
a=sum(np.array([10,20,30])*np.array([0.1,0.3,0.7]))
print(a)
28.0
a = sum(x*w for x,w in zip([10, 20, 30], [0.1, 0.3, 0.7]))
print(a)
28.0
网友评论