1. 有学号列表和姓名列表如下: id_student=[2019220301,2019220302,2019220303,2019220304,2019220305,2019220306,2019220307,2019220308] name=["Emma","Mike","Alice","Tony","Olivia","Allen","Christine","Make"] (1)用学号列表和姓名列表生成字典。(以学号为键,姓名为值) (2)编写函数find()实现功能:提供一个学号,判断该学生是否在字典中存在,如果字典中有该学号,则输出该学生的姓名;如果没有该学号,则输出提示"无此学号存在!"。
时间: 2023-07-07 12:32:22 浏览: 161
好的,我来依次回答你的问题:
(1) 生成字典的代码如下:
```python
id_student=[2019220301,2019220302,2019220303,2019220304,2019220305,2019220306,2019220307,2019220308]
name=["Emma","Mike","Alice","Tony","Olivia","Allen","Christine","Make"]
# 将学号列表和姓名列表组合成字典
student_dict = dict(zip(id_student, name))
print(student_dict)
```
输出结果如下:
```
{2019220301: 'Emma', 2019220302: 'Mike', 2019220303: 'Alice', 2019220304: 'Tony', 2019220305: 'Olivia', 2019220306: 'Allen', 2019220307: 'Christine', 2019220308: 'Make'}
```
(2) 实现 `find()` 函数的代码如下:
```python
def find(id, student_dict):
if id in student_dict:
print(student_dict[id])
else:
print("无此学号存在!")
# 测试函数
find(2019220301, student_dict) # 输出 Emma
find(2019220310, student_dict) # 输出 无此学号存在!
```
希望可以帮助到你。
阅读全文