def getSubjectStats(subject, weights):
'''
e.g.
subject:[['Bart','geology'],[10,5,85]]--grades即是[10,5,85]
weights:[0.3,0.2,0.5]
'''
return [[elt[0], elt[1], avg(elt[1], weights)]
for elt in subject]
def dotProduct(a,b):
result = 0.0
for i in range(len(a)):
result += a[i]*b[i]
return result
def avg(grades, weights):
try:
return dotProduct(grades, weights)/len(grades)#这几个函数就是为了求同学加权平均成绩,但是这里是不是错了?/len(grades)这个地方不对吧?加权平均数比如Bart地理成绩是[10,5,85],三个各占比例[0.3,0.2,0.5],那么应该是(10*0.3)+(5*0.2)+(85*0.5)就可以了啊,就算再除以那也是除以%100才对,怎么能再除以3??大神能否解答一下!
except ZeroDivisionError:
print 'no grades data'
return 0.0
添加回答
举报
0/150
提交
取消