编写程序,用zip()函数方式创建学生信息字典,包含学生姓名,学号,性别,身高
时间: 2024-05-05 11:18:00 浏览: 7
,体重和出生日期等信息。请问如何实现?
好的,您想要用zip函数创建字典。可以按照以下的方式编写程序:
```
names = ['Alice', 'Bob', 'Charlie']
numbers = [101, 102, 103]
genders = ['F', 'M', 'M']
heights = [165, 175, 180]
weights = [50, 70, 85]
birthdays = ['2000-01-01', '1999-05-06', '1998-11-11']
students = {}
for name, number, gender, height, weight, birthday in zip(names, numbers, genders, heights, weights, birthdays):
student_info = {}
student_info['name'] = name
student_info['number'] = number
student_info['gender'] = gender
student_info['height'] = height
student_info['weight'] = weight
student_info['birthday'] = birthday
students[number] = student_info
print(students)
```
这个程序首先定义了几个列表,分别存储学生的姓名、学号、性别、身高、体重和出生日期等信息。接下来,使用zip函数将这些列表拼接成一个整体,便于以一一对应的方式提取信息。
然后,通过循环遍历zip函数返回的内容,并将每个学生的信息存储到一个字典中,最后将这个字典以学生的学号为键存储到另一个字典中,完成了学生信息的创建。 最后,程序打印了包含所有学生信息的字典。