python 文件 成绩分析2

‘’’
文件score.txt中存储了学生的考试信息,内容如下
小明,98,96
小刚,90,94
小红,90,94
小王,98,96
小刘,80,90
小赵,90,96
第二列是数学成绩,第三列是语文成绩
请写程序分析:

  1. 哪些同学语文成绩是相同的?
  2. 哪些同学数学成绩是相同的?
  3. 哪些同学语文和数学成绩都是相同的?
  4. 总分最高的同学是谁,分数是多少?
  5. 总分的平均分是多少?

def read_score(filename):
	# 打开文件
	f = open(filename, 'r', encoding='utf-8')
	# 读取文件内容
	data = f.readlines()  # 一次读取所有行
	data = [i.strip().split(',') for i in data]  # strip()去除字符串两端的空格和换行符
	f.close()
	return data


def analyse_score():
	# 获取数据
	datas = read_score('score2.txt')
	# # 获取语文成绩列表
	# chinese_scores = [int(score[2]) for score in data] #[96, 94, 94, 96, 90, 96]
	stu_lst=[]
	for data in datas:
		name=data[0]
		math=int(data[1])
		chinese=int(data[2])
		stu_lst.append({'name':name,'math':math,'chinese':chinese}) #得到一个字典
	
	math_dict={}
	chinese_dict={}
	ys_dict={}
	max_score=0
	stu_score=0
	
	for stu in stu_lst:
		name=stu['name']
		math=stu['math']
		chinese=stu['chinese']
		score_sum=math+chinese #获取每次语文加数学的分数
		stu_score+=score_sum #得到总分
	
		if score_sum>max_score:
			max_score=score_sum #计算总分最高
		
		chinese_dict.setdefault(chinese,[])
		
		chinese_dict[chinese].append(name) #把语文成绩相同的同学名字添加到列表中
		
		math_dict.setdefault(math,[])
		
		math_dict[math].append(name) #把数学成绩相同的同学名字添加到列表中
		
		ys_dict.setdefault((math,chinese),[])
		ys_dict[(math,chinese)].append(name) #把语文和数学成绩相同的同学名字添加到列表中
	
	for k,v in chinese_dict.items():
		if len(v)>1:
			print(f'语文成绩相同的同学有:{v},他们的语文成绩是{k}')
	print('*'*20)
	for k,v in math_dict.items():
		if len(v)>1:
			print(f'数学成绩相同的同学有:{v},他们的数学成绩是{k}')
	print('*' * 20)
	
	for score,name_lst in ys_dict.items():
		sum_score=sum(score)
		if sum_score==max_score:
			print(f'总分最高的同学是:{name_lst},他们的总分是{max_score}')
		if len(name_lst)>1:
			print(f'语文和数学成绩相同的同学有:{name_lst},他们的总分是{max_score}')
			
	print(f'总分平均分是:{stu_score / len(stu_lst):.2f}')

if __name__ == '__main__':
	analyse_score()

在这里插入图片描述

相关推荐

  1. P8717 [蓝桥杯 2020 省 AB2] 成绩分析 Python

    2024-04-21 19:24:06       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 19:24:06       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 19:24:06       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 19:24:06       82 阅读
  4. Python语言-面向对象

    2024-04-21 19:24:06       91 阅读

热门阅读

  1. Git恢复至某一个提交的状态

    2024-04-21 19:24:06       34 阅读
  2. 【大模型系列】目录

    2024-04-21 19:24:06       35 阅读
  3. GoLang核心知识点

    2024-04-21 19:24:06       36 阅读
  4. Angular自定义异步表单验证

    2024-04-21 19:24:06       33 阅读
  5. Scala之面向对象 Day -3

    2024-04-21 19:24:06       44 阅读
  6. 134. 加油站

    2024-04-21 19:24:06       30 阅读
  7. 【设计模式】享元模式

    2024-04-21 19:24:06       40 阅读
  8. 批量控制教程-Ansible管理windows

    2024-04-21 19:24:06       41 阅读
  9. 【Ansible】02

    2024-04-21 19:24:06       35 阅读
  10. linux的工具tcptrace的输出信息的解释和介绍

    2024-04-21 19:24:06       38 阅读
  11. 【设计模式】7、decorate 装饰模式

    2024-04-21 19:24:06       34 阅读
  12. 黑马点评项目遇到的部分问题

    2024-04-21 19:24:06       124 阅读