创建一个学生选课字典,字典记录两名学生选课的信息,选择的课程用列表表示,创建完成后请根据学号从小到大输出学生信息。
时间: 2023-12-16 20:06:10 浏览: 87
好的,下面是代码实现:
```python
# 创建学生选课字典
student_courses = {
'001': ['Math', 'Physics', 'Chemistry'],
'002': ['Biology', 'History', 'Geography']
}
# 根据学号从小到大输出学生信息
sorted_students = sorted(student_courses.items(), key=lambda x: x[0])
for student in sorted_students:
print('Student ID:', student[0])
print('Selected Courses:', student[1])
print()
```
输出结果:
```
Student ID: 001
Selected Courses: ['Math', 'Physics', 'Chemistry']
Student ID: 002
Selected Courses: ['Biology', 'History', 'Geography']
```
这个程序创建了一个学生选课字典 `student_courses`,其中每个键都是学生的学号,对应的值是一个列表,表示该学生选的课程。然后使用 `sorted()` 函数对字典按照学号从小到大排序,并使用 `for` 循环遍历排序后的结果,输出每个学生的信息。
相关问题
创建一个学生选课字典,字典记录两名学生选课的信息,选择的课程用列表表示,创建完成后请根据学号从小到大输出学生信息
好的,以下是一个示例代码:
```python
student_courses = {
'001': ['Math', 'Physics', 'Chemistry'],
'003': ['English', 'History', 'Geography']
}
# 根据学号从小到大排序
sorted_students = sorted(student_courses.items(), key=lambda x: x[0])
for student, courses in sorted_students:
print(f"Student {student} has chosen the following courses: {', '.join(courses)}")
```
输出结果为:
```
Student 001 has chosen the following courses: Math, Physics, Chemistry
Student 003 has chosen the following courses: English, History, Geography
```
在这个示例代码中,我们创建了一个名为 `student_courses` 的字典,其中键为学生的学号,值为学生所选的课程列表。然后,我们使用 `sorted()` 函数按字典键从小到大排序,将排序后的元素存储在 `sorted_students` 列表中。最后,我们使用一个简单的循环遍历 `sorted_students` 列表,并打印出每个学生所选的课程。
3.创建一个学生选课字典,字典记录两名学生选课的信息,选择的课程用列表表示,创建完成后请根据学号从小到大输出学生信息。
好的,我可以使用Python语言来实现。代码如下:
```python
# 创建学生选课字典
stu_courses = {
"001": ["语文", "数学", "英语"],
"002": ["物理", "化学", "生物"]
}
# 根据学号从小到大输出学生信息
for stu_id in sorted(stu_courses.keys()):
print("学号:", stu_id)
print("课程:", stu_courses[stu_id])
```
程序中,我们首先创建了一个学生选课字典,其中包含两名学生的选课信息。然后,使用Python内置的`sorted`函数对学生选课字典的键(学号)进行排序,得到一个从小到大排列的学号列表。最后,遍历这个学号列表,依次输出每个学生的学号和选课信息。
运行结果如下:
```
学号: 001
课程: ['语文', '数学', '英语']
学号: 002
课程: ['物理', '化学', '生物']
```
阅读全文
相关推荐
















