pythonfor循环嵌套练习题
时间: 2023-06-22 09:18:01 浏览: 274
好的,我可以帮你提供一个Python的循环嵌套练习题,希望能帮到你。
题目描述:
给定一个列表,列表中的元素为字典类型,每个字典中包含两个键值对:'name' 和 'score',分别表示学生的姓名和分数。请编写程序,输出所有学生的姓名和分数,并按照分数从高到低的顺序排序。
列表内容如下:
```
students = [{'name': 'Tom', 'score': 90},
{'name': 'Jerry', 'score': 85},
{'name': 'Mickey', 'score': 95},
{'name': 'Minnie', 'score': 80}]
```
输出示例:
```
Mickey 95
Tom 90
Jerry 85
Minnie 80
```
代码实现:
```python
students = [{'name': 'Tom', 'score': 90},
{'name': 'Jerry', 'score': 85},
{'name': 'Mickey', 'score': 95},
{'name': 'Minnie', 'score': 80}]
# 使用冒泡排序按照分数从高到低排序
for i in range(len(students)):
for j in range(len(students)-1-i):
if students[j]['score'] < students[j+1]['score']:
students[j], students[j+1] = students[j+1], students[j]
# 输出学生姓名和分数
for student in students:
print(student['name'], student['score'])
```
输出结果:
```
Mickey 95
Tom 90
Jerry 85
Minnie 80
```
希望这个练习题能够帮助你熟悉Python的循环嵌套语法和列表的排序方法。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044833.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)