学生信息以字典形式存储,利用集合统计学生来自哪些专业
时间: 2024-05-12 20:17:16 浏览: 20
您好,我是 ChitGPT。对于您的问题,您可以使用以下代码来实现:
```python
# 定义学生信息字典
student_dict = {
"Tom": {"major": "Computer Science", "age": 20},
"Jerry": {"major": "Mathematics", "age": 22},
"Alice": {"major": "Physics", "age": 21},
"Bob": {"major": "Computer Science", "age": 19},
"Linda": {"major": "Mathematics", "age": 23},
}
# 利用 set 统计学生所在专业
major_set = set(info["major"] for info in student_dict.values())
print("学生来自以下专业:")
for major in major_set:
print(major)
```
运行以上代码后,即可输出所有学生所在专业的集合。希望我的回答能够帮到您!
相关问题
编写一个学生信息集合字典,并实现字典的功能
好的,我可以帮您实现这个功能。首先,我们需要定义一个字典来存储学生信息集合。我们可以使用学生的学号作为字典的键,学生的姓名、年龄、性别等信息作为对应的值。下面是一个简单的代码示例:
```python
student_dict = {}
# 添加学生信息
def add_student_info():
student_id = input("请输入学生学号:")
student_name = input("请输入学生姓名:")
student_age = input("请输入学生年龄:")
student_gender = input("请输入学生性别:")
student_dict[student_id] = {
"姓名": student_name,
"年龄": student_age,
"性别": student_gender
}
print("学生信息添加成功!")
# 查找学生信息
def search_student_info():
student_id = input("请输入要查找的学生学号:")
if student_id in student_dict:
print("学号:{}".format(student_id))
for key, value in student_dict[student_id].items():
print("{}:{}".format(key, value))
else:
print("该学生不存在!")
# 修改学生信息
def modify_student_info():
student_id = input("请输入要修改的学生学号:")
if student_id in student_dict:
for key, value in student_dict[student_id].items():
print("{}:{}".format(key, value))
field = input("请输入要修改的字段:")
if field in student_dict[student_id]:
value = input("请输入修改后的值:")
student_dict[student_id][field] = value
print("学生信息修改成功!")
else:
print("该字段不存在!")
else:
print("该学生不存在!")
# 删除学生信息
def delete_student_info():
student_id = input("请输入要删除的学生学号:")
if student_id in student_dict:
del student_dict[student_id]
print("学生信息删除成功!")
else:
print("该学生不存在!")
# 显示所有学生信息
def show_all_student_info():
if len(student_dict) == 0:
print("暂无学生信息!")
else:
for student_id, student_info in student_dict.items():
print("学号:{}".format(student_id))
for key, value in student_info.items():
print("{}:{}".format(key, value))
print()
# 主函数
def main():
while True:
print("=" * 30)
print("学生信息管理系统")
print("1. 添加学生信息")
print("2. 查找学生信息")
print("3. 修改学生信息")
print("4. 删除学生信息")
print("5. 显示所有学生信息")
print("0. 退出系统")
print("=" * 30)
choice = input("请输入您的选择:")
if choice == "1":
add_student_info()
elif choice == "2":
search_student_info()
elif choice == "3":
modify_student_info()
elif choice == "4":
delete_student_info()
elif choice == "5":
show_all_student_info()
elif choice == "0":
print("感谢使用学生信息管理系统,再见!")
break
else:
print("输入有误,请重新输入!")
if __name__ == "__main__":
main()
```
以上代码实现了学生信息管理的基本功能,包括添加、查找、修改、删除和显示所有学生信息。您可以根据需要进行修改和扩展。
学生信息以字典格式存入列表中后,查询学生信息
好的,我理解了你的问题。如果你已经将学生信息以字典格式存入列表中,那么你可以通过遍历这个列表来查找特定的学生信息。
以下是一个示例代码:
```python
students = [
{"name": "Alice", "age": 20, "major": "Computer Science"},
{"name": "Bob", "age": 19, "major": "Mathematics"},
{"name": "Charlie", "age": 21, "major": "Physics"}
]
# 查询名为Alice的学生信息
target_name = "Alice"
for student in students:
if student["name"] == target_name:
print(student)
break
```
在上面的代码中,我们定义了一个包含三个学生信息的列表,然后遍历这个列表,如果发现某个学生的名字和我们要查询的名字匹配,就输出该学生的信息。
你可以根据这个示例代码,自行修改来满足你的具体需求。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)