现有某班学生的学号、姓名、计算机成绩如下,请定义字典后,编程对该班学生计算机成绩降序排列。输出结果请竖着排列
时间: 2024-03-17 14:39:48 浏览: 221
可以使用 Python 的字典和排序功能来实现,并且可以使用字符串格式化的方式竖着排列输出:
```python
# 定义班级学生信息字典
students = {
"190001": "张三",
"190002": "李四",
"190003": "王五",
"190004": "赵六",
}
# 定义学生计算机成绩字典
computer_scores = {
"190001": 80,
"190002": 90,
"190003": 70,
"190004": 85,
}
# 按计算机成绩降序排列
sorted_scores = sorted(computer_scores.items(), key=lambda x: x[1], reverse=True)
# 输出结果
for score in sorted_scores:
student_id = score[0]
student_name = students[student_id]
computer_score = score[1]
print("{:<7}{:<5}{:<3}".format(student_id, student_name, computer_score))
```
输出结果:
```
190002 李四 90
190004 赵六 85
190001 张三 80
190003 王五 70
```
其中,`{:<7}`表示左对齐且占7个字符的学号,`{:<5}`表示左对齐且占5个字符的姓名,`{:<3}`表示左对齐且占3个字符的计算机成绩。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)