Python列表删除语数外总分最低的那条学生信息
时间: 2023-07-16 16:16:24 浏览: 50
假设每个学生信息都是一个字典,包含学生的姓名、语文成绩、数学成绩和外语成绩,可以按照以下步骤删除语数外总分最低的学生信息:
1. 定义一个列表,保存所有学生信息。
```python
students = [
{'name': '张三', 'chinese': 80, 'math': 90, 'english': 70},
{'name': '李四', 'chinese': 85, 'math': 80, 'english': 75},
{'name': '王五', 'chinese': 70, 'math': 60, 'english': 80}
]
```
2. 遍历列表,计算每个学生的总分,并找到总分最低的学生。
```python
lowest_score = float('inf')
lowest_score_student = None
for student in students:
total_score = student['chinese'] + student['math'] + student['english']
if total_score < lowest_score:
lowest_score = total_score
lowest_score_student = student
```
3. 如果找到了总分最低的学生,则从列表中删除该学生的信息。
```python
if lowest_score_student is not None:
students.remove(lowest_score_student)
```
完整代码如下所示:
```python
students = [
{'name': '张三', 'chinese': 80, 'math': 90, 'english': 70},
{'name': '李四', 'chinese': 85, 'math': 80, 'english': 75},
{'name': '王五', 'chinese': 70, 'math': 60, 'english': 80}
]
lowest_score = float('inf')
lowest_score_student = None
for student in students:
total_score = student['chinese'] + student['math'] + student['english']
if total_score < lowest_score:
lowest_score = total_score
lowest_score_student = student
if lowest_score_student is not None:
students.remove(lowest_score_student)
print(students)
```
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)