利用合适的数据结构存储学生姓名,利用ndarry数组随机生成班上每个同学的三门成绩,比如Python、math、English。成绩范围为40~100,使用统计函数计算每门课程的成绩的最高分、最低分、平均分、标准差;打印班级三门课程总成绩的最高分、最低分的学生名字
时间: 2023-07-10 14:34:59 浏览: 70
好的,我可以为您解答这个问题。
首先,我们可以使用 Python 的字典数据结构来存储学生姓名和成绩信息。例如:
```python
students = {
"Alice": [85, 92, 78],
"Bob": [76, 88, 91],
"Charlie": [90, 84, 85],
# ... 其他学生的信息
}
```
接下来,我们可以使用 NumPy 库中的 `random.randint()` 函数生成随机成绩。例如:
```python
import numpy as np
# 生成 20 名学生的成绩
num_students = 20
grades = np.random.randint(40, 101, size=(num_students, 3))
```
这里,`np.random.randint(40, 101, size=(num_students, 3))` 表示生成 20 行 3 列的数组,每个元素的值在 40 到 100 之间。
接下来,我们可以使用 NumPy 库中的统计函数计算每门课程的成绩的最高分、最低分、平均分、标准差。例如:
```python
# 计算每门课程的最高分、最低分、平均分、标准差
max_grades = np.max(grades, axis=0)
min_grades = np.min(grades, axis=0)
avg_grades = np.mean(grades, axis=0)
std_grades = np.std(grades, axis=0)
```
这里,`np.max(grades, axis=0)` 表示沿着第 0 轴(即列)计算最大值。
最后,我们可以使用 NumPy 库中的 `argmax()` 和 `argmin()` 函数找到总成绩的最高分、最低分的学生名字。例如:
```python
# 计算每个学生的总成绩
total_grades = np.sum(grades, axis=1)
# 找到总成绩的最高分、最低分的学生名字
max_idx = np.argmax(total_grades)
min_idx = np.argmin(total_grades)
max_student = list(students.keys())[max_idx]
min_student = list(students.keys())[min_idx]
```
这里,`np.argmax(total_grades)` 表示找到总成绩最高的学生在数组中的索引,`list(students.keys())[max_idx]` 表示找到该索引对应的学生名字。
最终,我们可以打印出每门课程的最高分、最低分、平均分、标准差,以及总成绩的最高分、最低分的学生名字。例如:
```python
print("Python: max = {}, min = {}, avg = {:.2f}, std = {:.2f}".format(
max_grades[0], min_grades[0], avg_grades[0], std_grades[0]))
print("Math: max = {}, min = {}, avg = {:.2f}, std = {:.2f}".format(
max_grades[1], min_grades[1], avg_grades[1], std_grades[1]))
print("English: max = {}, min = {}, avg = {:.2f}, std = {:.2f}".format(
max_grades[2], min_grades[2], avg_grades[2], std_grades[2]))
print("Highest total grade: {} ({} points)".format(max_student, total_grades[max_idx]))
print("Lowest total grade: {} ({} points)".format(min_student, total_grades[min_idx]))
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)