错误<generator object =“”get_average_rating。<locals =“”>。<genexpr> at 0x000000000156F728&gt;

时间:2018-05-03 15:33:22

标签: python python-3.x

尝试打印此

的结果
average1 = 3.82 
average2 = 3.98 

short_rating1 = [3.5, 4.5, 4.0, 3.5, 4.0, 4.5, 3.5, 4.0, 2.0, 4.0, 3.0, 5.0, 4.5, 4.5, 3.0, 3.5, 4.0, 4.5, 2.0, 5.0, 3.0, 4.0, 3.0, 4.0, 5.0] 
short_rating2 = [3.5, 4.5, 5.0, 4.5, 5.0, 3.5, 4.0, 4.5, 3.5, 4.5, 3.0, 3.5, 3.0, 5.0, 4.0, 4.5, 4.0, 4.0, 3.0, 3.5, 4.0, 4.0, 4.0, 4.5, 3.0] 

denominator = (math.sqrt(sum((a - average1) ** 2)) * math.sqrt(sum(((b - average2) ** 2))) for a, b in zip(short_rating1, short_rating2))

print(denominator)

1 个答案:

答案 0 :(得分:0)

sum((a - average1) ** 2)sum(((b - average2) ** 2))是您的问题。 sum是一个函数,它接受一个iterable并对其元素求和。 (a - average1) ** 2是一个浮点数,所以你得到一个错误。删除sum来电

denominator = (math.sqrt((a - average1) ** 2) * math.sqrt((b - average2) ** 2) for a, b in zip(short_rating1, short_rating2))
list(denominator)
# [0.15359999999999993, 0.3536000000000001, 0.18360000000000018, 0.1663999999999999, 0.18360000000000018, 0.3264000000000001, 0.006400000000000003, 0.09360000000000009, 0.8735999999999999, 0.09360000000000009, 0.8035999999999999, 0.5664, 0.6664000000000001, 0.6936000000000002, 0.016400000000000012, 0.1663999999999999, 0.0036000000000000064, 0.013600000000000015, 1.7835999999999999, 0.5664, 0.016400000000000012, 0.0036000000000000064, 0.016400000000000012, 0.09360000000000009, 1.1564]