这是代码:for i in range(0, len(my_list), 6):
print(my_list [i] + " " + my_list [i+1] + " - " + my_list [i+2] + " - " + my_list [i+3] + " , " + my_list [i+4] + " , " + my_list [i+5])所以这段代码需要一个文件,并将文件中的 6 行打印成一行,格式如下:Marilyn Munro - 65783234 - 66, 77, 89
Johnny Lang - 83492753 - 45, 65, 45我希望代码对最后三个数字求平均值,以便输出:James Hanniburg - 359372932 - 50, 75, 100对此:James Hanniburg - 359372932 - 81.25 #81.25 being the average of 50, 75, and 100, assuming 100 is worth 50%, and 50 and 75 are each worth 25%.编辑:我忘了提及,第三个值的价值为 50%,而其他两个价值各为 25%。有什么线索吗??
3 回答
BIG阳
TA贡献1859条经验 获得超6个赞
试试这个:(已添加值权重。)
my_list = ['James', 'Hanniburg', 359372932, 50, 75, 100] # just input
a, b, c, d, e, f = my_list
print('{0} {1} - {2} - {3}'.format(a,b,c,((d*0.25+e*0.25+f*0.5)/3)))
输出:
C:\Users\Desktop>py x.py
James Hanniburg - 359372932 - 22.916666666666668
您还可以从统计中导入均值:
my_list = ['James', 'Hanniburg', 359372932, 50, 75, 100]
from statistics import mean
a, b, c, *d = my_list
print('{0} {1} - {2} - {3}'.format(a,b,c,mean(d)))
添加回答
举报
0/150
提交
取消