假设有一张成绩表记录了10名学生的语文、数学、英语、生物、历史这5门成绩,成绩范围均为50-100分。10名学生的学号为100,101,102,103,104,105,106,107,108,109。利用numpy完成以下操作:(1)使用随机数模拟学生成绩。(2)查询学号为205的学生的英语成绩。(3)查询学号为100,102,105,109的四位学生的语文、数学和英语成绩。(4)查询大于或者等于90分的成绩和相应学生的学号。(5)按各门课程的成绩排序。(6)按每名学生的成绩排序。(7)计算每门课程的平均分、最高分和最低分。(8)计算每名学生的最高分和最低分。(9)查询最低分及相应学生的学号和课程。(10)查询最高分及相应的学生学号和课程。(11)语文、数学、英语、生物、历史这5门课程在总分中的占比分别为25%、25%、20%、15%、15%。如果总分为100分,则计算每名学生的总成绩。(12)查询最高的3个总分
时间: 2024-02-09 11:12:08 浏览: 32
的学生及其学号。
回答:
(1) 使用随机数模拟学生成绩。
```python
import numpy as np
# 随机生成10名学生的成绩
np.random.seed(0)
scores = np.random.randint(50, 101, size=(10, 5))
# 学生学号
students = np.array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])
print("10名学生的成绩为:\n", scores)
```
(2) 查询学号为105的学生的英语成绩。
```python
english_score = scores[5, 2]
print("学号为105的学生的英语成绩为:", english_score)
```
(3) 查询学号为100,102,105,109的四位学生的语文、数学和英语成绩。
```python
selected_scores = scores[[0, 2, 5, 9]][:, :3]
print("学号为100,102,105,109的四位学生的语文、数学和英语成绩为:\n", selected_scores)
```
(4) 查询大于或者等于90分的成绩和相应学生的学号。
```python
high_scores = np.where(scores >= 90)
for i in range(len(high_scores[0])):
print("学号为{}的学生在{}科目中获得{}分".format(students[high_scores[0][i]],
['语文', '数学', '英语', '生物', '历史'][high_scores[1][i]],
scores[high_scores[0][i], high_scores[1][i]]))
```
(5) 按各门课程的成绩排序。
```python
sorted_scores = np.sort(scores, axis=0)[::-1]
print("按各门课程的成绩排序:\n", sorted_scores)
```
(6) 按每名学生的成绩排序。
```python
sorted_scores = np.sort(scores, axis=1)[::-1]
print("按每名学生的成绩排序:\n", sorted_scores)
```
(7) 计算每门课程的平均分、最高分和最低分。
```python
print("每门课程的平均分、最高分和最低分为:")
for i, subject in enumerate(['语文', '数学', '英语', '生物', '历史']):
subject_scores = scores[:, i]
print("{}:平均分{},最高分{},最低分{}".format(subject, np.mean(subject_scores), np.max(subject_scores), np.min(subject_scores)))
```
(8) 计算每名学生的最高分和最低分。
```python
print("每名学生的最高分和最低分为:")
for i, student in enumerate(students):
student_scores = scores[i]
print("学号为{}的学生:最高分{},最低分{}".format(student, np.max(student_scores), np.min(student_scores)))
```
(9) 查询最低分及相应学生的学号和课程。
```python
min_score = np.min(scores)
min_indices = np.argwhere(scores == min_score)
for i in range(len(min_indices)):
print("学号为{}的学生在{}科目中获得最低分{}".format(students[min_indices[i][0]],
['语文', '数学', '英语', '生物', '历史'][min_indices[i][1]],
min_score))
```
(10) 查询最高分及相应的学生学号和课程。
```python
max_score = np.max(scores)
max_indices = np.argwhere(scores == max_score)
for i in range(len(max_indices)):
print("学号为{}的学生在{}科目中获得最高分{}".format(students[max_indices[i][0]],
['语文', '数学', '英语', '生物', '历史'][max_indices[i][1]],
max_score))
```
(11) 语文、数学、英语、生物、历史这5门课程在总分中的占比分别为25%、25%、20%、15%、15%。如果总分为100分,则计算每名学生的总成绩。
```python
weights = np.array([0.25, 0.25, 0.2, 0.15, 0.15])
total_scores = np.dot(scores, weights) / np.sum(weights) * 100
for i, student in enumerate(students):
print("学号为{}的学生总成绩为{}".format(student, total_scores[i]))
```
(12) 查询最高的3个总分的学生及其学号。
```python
top_k = 3 # 查询前3名
idx = np.argsort(total_scores)[::-1][:top_k]
for i in range(top_k):
print("第{}名学生:学号为{},总成绩为{}".format(i+1, students[idx[i]], total_scores[idx[i]]))
```