python去掉最高分和最低分怎么算平均分_python – 存储最后3个分数并删除旧分数并计算平均值?...

这段代码展示了如何使用Python处理CSV文件,根据学生姓名分组分数,并只保留每个学生的最后三个分数。然后,它计算并打印出按字母顺序排列的学生最高分、按最高分降序排列的学生最高分,以及按平均分降序排列的学生平均分。
摘要由CSDN通过智能技术生成

我将给出一些演示代码:

# -*- coding: utf-8 -*-

import csv

from collections import defaultdict

from statistics import mean

class_file = 'scores.csv'

open_file = open(class_file)

csv_file = csv.reader(open_file)

def main():

# First, use student name to group by all scores, this will

# generate structure like this:

# {

# 'Andrew': [1, 2, 3, 4, 5]),

# 'Luck': [10, 20]),

# }

score_groups = defaultdict(list)

for name, score in csv_file:

score_groups[name].append(int(score))

# Secondary, use the 3 latest socres only

l3_score_groups = [(key, value[-3:]) for key, value in score_groups.items()]

print('1. Alphabetical order with each students highest score.')

l3_highest_score_groups = [(key, max(values)) for key, valu

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值