python读取文件中的数值并计算平均值

[复制链接]
查看109 | 回复0 | 2024-2-29 08:22:31 | 显示全部楼层 |阅读模式

登录网站,浏览更多精彩内容

您需要 登录 才可以下载或查看,没有账号?加入我们

×
p082.txt文件内容

101,小张,88
102,小王,77
103,小李,99
104,小强,66
105,小赵,55
python代码知识点:四舍五入round函数、去除字符串首尾空格函数strip、把字符串转化为数组函数split("字符串", "分隔符“)

[mw_shl_code=python,true]scores = []
with open("p082.txt") as f:
    for i in f:
        line = i.strip()
        fields = line.split(",")
        scores.append(int(fields[-1]))
max_score = max(scores)
min_score = min(scores)
avg_score = round(sum(scores) / len(scores),2)
print(max_score, min_score, avg_score)[/mw_shl_code]



中国领先的数字技术资源交流中心!

30

主题

10

回帖

275

积分

学徒

积分
275
学费
218