编写一个函数,向stus_list = [{"id": "0001", "name": "张三", "age": 18, "classes": "java","grades":100}, {"id": "0002", "name": "李四", "age": 23, "classes": "java","grades":100}, {"id": "0003", "name": "王五", "age": 20, "classes": "java","grades":100}]中添加新的学员信息
时间: 2023-07-07 12:44:53 浏览: 96
好的,以下是添加新学员信息的 Python 代码:
```Python
def add_student(stus_list, new_student):
stus_list.append(new_student)
return stus_list
```
其中,`stus_list` 表示已有学员信息的列表,`new_student` 表示新学员的信息,包括学号、姓名、年龄、班级和成绩等。使用 `append()` 方法将新学员信息添加到学员列表中,并返回更新后的学员列表。
例如,可以这样调用该函数来添加新学员信息:
```Python
stus_list = [{"id": "0001", "name": "张三", "age": 18, "classes": "java","grades":100},
{"id": "0002", "name": "李四", "age": 23, "classes": "java","grades":100},
{"id": "0003", "name": "王五", "age": 20, "classes": "java","grades":100}]
new_student = {"id": "0004", "name": "赵六", "age": 22, "classes": "python","grades":95}
updated_stus_list = add_student(stus_list, new_student)
print(updated_stus_list)
```
输出结果为:
```
[{'id': '0001', 'name': '张三', 'age': 18, 'classes': 'java', 'grades': 100},
{'id': '0002', 'name': '李四', 'age': 23, 'classes': 'java', 'grades': 100},
{'id': '0003', 'name': '王五', 'age': 20, 'classes': 'java', 'grades': 100},
{'id': '0004', 'name': '赵六', 'age': 22, 'classes': 'python', 'grades': 95}]
```
可以看到,新学员信息已经成功添加到了学员列表中。
阅读全文