定义:若有n个数 的权分别是 ,那么
叫做这n个数的加权平均值。如果权重相同,加权平均值即为平均值
def weighted_average(datalist,weigth=1):numerator=0if weigth !=1:denominator=sum(weigth)assert len(datalist) == len(weigth), "length not equal"for i in range(len(datalist)):numerator+=datalist[i]*weigth[i]else:denominator = len(datalist)numerator=sum(datalist)return numerator/denominatorif __name__=='__main__':datalist=list(range(1,11))weigth=list(range(10,0,-1))num=weighted_average(datalist,weigth)print(num)