-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
profile = {'name': '胡歌', 'age': 18, 'scores': 98, 'call': '086-88966', 'gender': '男'}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
student_profile = [ {'name': '小贝', 'age': 18, 'scores': 98, 'call': '18688961236', 'gender': '男'}, {'name': '小夕', 'age': 17, 'scores': 95, 'call': '18688912468', 'gender': '女'}, {'name': '小乐', 'age': 16, 'scores': 82, 'call': '18688971256', 'gender': '女'}, {'name': '小明', 'age': 18, 'scores': 76, 'call': '18688951268', 'gender': '男'}, {'name': '小美', 'age': 17, 'scores': 56, 'call': '18688931276', 'gender': '女'}, {'name': '小菊', 'age': 16, 'scores': 54, 'call': '18688941286', 'gender': '不明'} ]
-
统计不及格学生的个数
count = 0 for i in student_profile: if i['scores'] < 60: count += 1 print(count)
-
打印不及格未成年学生的名字和对应的成绩
for i in student_profile: if i['scores'] < 60: print(i['name'], i['scores'])
-
求所有男生的平均年龄
all_age = 0 man_student = 0 for i in student_profile: if i['gender'] == '男': man_student += 1 all_age += i['age'] print(f'所有男生的平均年龄:{all_age / man_student}')
-
打印手机尾号是8的学生的名字
call8_name = [i['name'] for i in student_profile if int(i['call']) % 10 == 8] print(call8_name)
-
打印最高分和对应的学生的名字
all_scores = [i['scores'] for i in student_profile] for i in student_profile: if i['scores'] == max(all_scores): print(i['name'], max(all_scores))
-
删除性别不明的所有学生
for i in student_profile: if i['gender'] == '不明': student_profile.pop() print(student_profile)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
-
已知一个列表保存了多个狗对应的字典:
dogs = [ {'name': '贝贝', 'color': '白色', 'breed': '银狐', 'age': 3, 'gender': '母'}, {'name': '花花', 'color': '灰色', 'breed': '法斗', 'age': 2}, {'name': '财财', 'color': '黑色', 'breed': '土狗', 'age': 5, 'gender': '公'}, {'name': '包子', 'color': '黄色', 'breed': '哈士奇', 'age': 1}, {'name': '可乐', 'color': '白色', 'breed': '银狐', 'age': 2}, {'name': '旺财', 'color': '黄色', 'breed': '土狗', 'age': 2, 'gender': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
all_breed = [i['breed'] for i in dogs] print(all_breed)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
white_good_name = [i['name'] for i in dogs if i['color'] == '白色'] print(white_good_name)
-
给dogs中没有性别的狗添加性别为 ‘公’
for i in dogs: i.setdefault('gender', '公') print(dogs)
-
统计 ‘银狐’ 的数量
count = 0 for i in dogs: if i['breed'] == '银狐': count += 1 print(count)
-
day8-字典作业
最新推荐文章于 2023-11-08 18:39:48 发布