-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student = { 'name': '小兰', 'age': 18, 'score': 92, 'tel': '18989899988', 'gender': '女' } print(student) # {'name': '小兰', 'age': 18, 'score': 92, 'tel': '18989899988', 'gender': '女'}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
-
统计不及格学生的个数
-
打印不及格未成年学生的名字和对应的成绩
-
求所有男生的平均年龄
-
打印手机尾号是8的学生的名字
-
打印最高分和对应的学生的名字
-
删除性别不明的所有学生
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students = [ {'name': '小兰', 'age': 17, 'score': 82, 'tel': '18989899988', 'gender': '女' }, {'name': '小紫', 'age': 18, 'score': 88, 'tel': '15656566655', 'gender': '女' }, {'name': '小明', 'age': 19, 'score': 75, 'tel': '18778788877', 'gender': '男' }, {'name': '小杨', 'age': 20, 'score':58, 'tel': '15545544455', 'gender': '不明' }, {'name': '小白', 'age': 17, 'score': 55, 'tel': '19191199911', 'gender': '男' }, {'name': '小黄', 'age': 19, 'score': 67, 'tel': '17767876878', 'gender': '不明' } ] # 1)统计不及格学生的个数 count = 0 for stu in students: if stu['score'] < 60: count += 1 print(count) # 2 print('------------------------------华丽分割线-----------------------------') # 2)打印不及格未成年学生的名字和对应的成绩 for stu in students: if stu['score'] < 60 and stu['age'] < 18: print('不及格未成年学生:', stu['name'], stu['score']) print('------------------------------华丽分割线-----------------------------') # 3)求所有男生的平均年龄 count = 0 sum_age = 0 for stu in students: if stu['gender'] == '男': count += 1 sum_age += stu['age'] print('所有男生的平均年龄:', sum_age/count) print('------------------------------华丽分割线-----------------------------') # 4) 打印手机尾号是8的学生的名字 for stu in students: if stu['tel'][-1] == '8': print('尾号是8的学生:', stu['name']) print('------------------------------华丽分割线-----------------------------') # 5) 打印最高分和对应的学生的名字 max1 = students[0]['score'] name = students[0]['name'] for stu in students: if max1 < stu['score']: max1 = stu['score'] name = stu['name'] print(name, max1) print('------------------------------华丽分割线-----------------------------') # 6)删除性别不明的所有学生 for stu in students: if stu['gender'] == '不明': students.remove(stu) print(students) print('------------------------------华丽分割线-----------------------------') # 7)将列表按学生成绩从大到小排序(挣扎一下,不行就放弃) new_students = [] for x in students: new_students = sorted(students,key=lambda x : x['score'],reverse=True) print(new_students)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = { 'name': 'python 2301', 'location': '21教', 'lecturer': {'name': '余婷', 'age': '女', 'tel': '13678192302'}, 'head teacher': {'name': '萍姐', 'age': '18', 'tel': '110'}, 'all students': [ {'name': 'stu1', 'age': 18, 'tel': 120, '专业': '电子商务', 'score': 100, 'linkman': {'name': '张三', 'tel': '768'} }, {'name': 'stu2', 'age': 17, 'tel': 121, '专业': '计算机', 'score': 90, 'linkman': {'name': '李四', 'tel': '458'} }, {'name': 'stu3', 'age': 20, 'tel': 124, '专业': '数学', 'score': 94, 'linkman': {'name': '王五', 'tel': '367'} }, {'name': 'stu4', 'age': 16, 'tel': 123, '专业': '数学', 'score': 97, 'linkman': {'name': '小明', 'tel': '256'} }, ] }
-
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
-
给dogs中没有性别的狗添加性别为 ‘公’
-
统计 ‘银狐’ 的数量
# 1)利用列表推导式获取所有狗的品种 result = [x['breed'] for x in dogs] print(result) # ['银狐', '法斗', '土狗', '哈士奇', '银狐', '土狗'] print('------------------------------华丽分割线-----------------------------') # 2)利用列表推导式获取所有白色狗的名字 result = [x['name'] for x in dogs if x['color'] == '白色'] print(result) # ['贝贝', '可乐'] print('------------------------------华丽分割线-----------------------------') # 3)给dogs中没有性别的狗添加性别为 '公' for x in dogs: x.setdefault('gender', '公') print(dogs) print('------------------------------华丽分割线-----------------------------') # 4)统计 ‘银狐’ 的数量 count = 0 for x in dogs: if x['breed'] == '银狐': count += 1 print(count) # 2
-
【作业】字典
于 2023-03-15 20:46:51 首次发布