1.假设有一张成绩表记录了10名学生的语文、数学、英语、生物、历史这5门课的成绩,成绩范围均为50~100分。10名学生的学号分别为100、101、102、103、104、105、106、107、108、109。利用numpy完成一下操作: (1)使用随机数模拟学生成绩,并存储在数组中。 (2)查询学号为105的学生的英语成绩。 (3)查询学号为100、102、105、109的4位学生的语文、数学和英语成绩。 (4)查询大于或等于90分的成绩和相应学生的学号。 (5)按各门课程的成绩排序。 (6)按每名学生的成绩排序。 (7)计算每门课程的平均分、最高分和最低分。 (8)计算每名学生的最高分、最低分。 (9)查询最低分及相应的学生学号和课程。 (10)查询最高分及相应的学生学号和课程。 (11)语文、数学、英语、生物、历史这5门课程在总分中的占比分别为25%、25%、20%、15%、15%。如果总分位100分,则计算每名学生的总成绩。 (12)查询最高的3个总分。
时间: 2023-07-10 07:11:00 浏览: 71
好的,以下是你的问题的回答:
(1)使用随机数模拟学生成绩,并存储在数组中。
```python
import numpy as np
# 随机生成10名学生的5门成绩
scores = np.random.randint(50, 101, size=(10, 5))
# 存储10名学生的学号
student_ids = np.array([100, 101, 102, 103, 104, 105, 106, 107, 108, 109])
```
(2)查询学号为105的学生的英语成绩。
```python
english_score = scores[np.where(student_ids == 105)[0], 2][0]
print("学号为105的学生的英语成绩为:", english_score)
```
(3)查询学号为100、102、105、109的4位学生的语文、数学和英语成绩。
```python
selected_scores = scores[np.where(np.isin(student_ids, [100, 102, 105, 109]))][:, :3]
print("学号为100、102、105、109的学生的语文、数学和英语成绩为:\n", selected_scores)
```
(4)查询大于或等于90分的成绩和相应学生的学号。
```python
# 先获取大于或等于90分的成绩的下标
high_scores_indices = np.where(scores >= 90)
# 再根据下标获取相应的学生学号和成绩
high_scores = [(student_ids[i], scores[i, j]) for i, j in zip(high_scores_indices[0], high_scores_indices[1])]
print("大于或等于90分的成绩和相应学生的学号为:", high_scores)
```
(5)按各门课程的成绩排序。
```python
sorted_scores = np.sort(scores, axis=0)
print("按各门课程的成绩排序后的成绩为:\n", sorted_scores)
```
(6)按每名学生的成绩排序。
```python
sorted_scores = np.sort(scores, axis=1)
print("按每名学生的成绩排序后的成绩为:\n", sorted_scores)
```
(7)计算每门课程的平均分、最高分和最低分。
```python
# 计算每门课程的平均分、最高分和最低分
mean_scores = np.mean(scores, axis=0)
max_scores = np.max(scores, axis=0)
min_scores = np.min(scores, axis=0)
# 输出结果
print("每门课程的平均分为:", mean_scores)
print("每门课程的最高分为:", max_scores)
print("每门课程的最低分为:", min_scores)
```
(8)计算每名学生的最高分、最低分。
```python
# 计算每名学生的最高分和最低分
max_scores = np.max(scores, axis=1)
min_scores = np.min(scores, axis=1)
# 输出结果
for i, student_id in enumerate(student_ids):
print("学号为{}的学生的最高分为{},最低分为{}".format(student_id, max_scores[i], min_scores[i]))
```
(9)查询最低分及相应的学生学号和课程。
```python
# 先获取最低分的下标
min_score_indices = np.argwhere(scores == np.min(scores))
# 再根据下标获取相应的学生学号和课程
min_scores = [(student_ids[i], j) for i, j in zip(min_score_indices[:, 0], min_score_indices[:, 1])]
print("最低分及相应的学生学号和课程为:", min_scores)
```
(10)查询最高分及相应的学生学号和课程。
```python
# 先获取最高分的下标
max_score_indices = np.argwhere(scores == np.max(scores))
# 再根据下标获取相应的学生学号和课程
max_scores = [(student_ids[i], j) for i, j in zip(max_score_indices[:, 0], max_score_indices[:, 1])]
print("最高分及相应的学生学号和课程为:", max_scores)
```
(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.sum(scores * weights, axis=1)
# 输出结果
for i, student_id in enumerate(student_ids):
print("学号为{}的学生的总成绩为{}".format(student_id, total_scores[i]))
```
(12)查询最高的3个总分。
```python
# 获取总成绩从大到小排序后的下标
sorted_indices = np.argsort(total_scores)[::-1]
# 取前3个总成绩最高的学生的学号和总成绩
top_3_scores = [(student_ids[i], total_scores[i]) for i in sorted_indices[:3]]
print("最高的3个总分为:", top_3_scores)
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)