用Python编写下述程序。有一个关于某门课程期中考试评分结果的mid.csv文件。第一行是标题。然后,每一行显示学生的注册姓名和分数。编写程序读取该文件并将所有分数收集到一个列表中(没有标题行)根据习题集1中问题2给出的标准对这些分数进行分类。然后,用plot绘制等级A、B、C、D、F的直方图。
时间: 2023-07-15 21:13:50 浏览: 34
以下是程序的代码:
```python
import csv
import matplotlib.pyplot as plt
# 打开文件并读取数据
with open('mid.csv', 'r') as file:
reader = csv.reader(file)
next(reader) # 跳过标题行
scores = [int(row[1]) for row in reader]
# 对分数进行分类
grade_A = [score for score in scores if score >= 90]
grade_B = [score for score in scores if 80 <= score < 90]
grade_C = [score for score in scores if 70 <= score < 80]
grade_D = [score for score in scores if 60 <= score < 70]
grade_F = [score for score in scores if score < 60]
# 绘制直方图
grades = ['A', 'B', 'C', 'D', 'F']
counts = [len(grade_A), len(grade_B), len(grade_C), len(grade_D), len(grade_F)]
plt.bar(grades, counts)
plt.title('Grades Distribution')
plt.xlabel('Grade')
plt.ylabel('Count')
plt.show()
```
上述代码首先打开文件,并使用csv模块读取数据。然后,使用列表推导式将所有分数收集到一个列表中,并根据给定的标准进行分类。最后,使用matplotlib库中的plot函数绘制等级A、B、C、D、F的直方图。