-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
stu_ = { 'name': '小明', 'age': '18', 'score_math': 88, 'tel': '18858821154', '性别': '男' }
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [ {'name': '小明', 'age': 18, 'score_math': 88, 'tel': '18858821154', '性别': '男'}, {'name': '小黄', 'age': 22, 'score_math': 32, 'tel': '15558821158', '性别': '男'}, {'name': '小花', 'age': 17, 'score_math': 75, 'tel': '17758841458', '性别': '女'}, {'name': '小小', 'age': 97, 'score_math': 99, 'tel': '16258771455', '性别': '不明'}, {'name': '大大', 'age': 99, 'score_math': 22, 'tel': '12254171447', '性别': '不明'}, {'name': '小白', 'age': 25, 'score_math': 78, 'tel': '11158821141', '性别': '男'} ]
-
统计不及格学生的个数
count = 0 for x in students: if x['score_math'] < 60: count += 1 print(count)
-
打印不及格未成年学生的名字和对应的成绩
loser = [(x["name"], x["score_math"]) for x in students if x['score_math'] < 60] print('不及格学生有:', loser)
-
求所有男生的平均年龄
average_man = sum([x['age'] for x in students if x['性别'] == '男']) / len([x['score_math'] for x in students if x['性别'] == '男']) print(average_man)
-
打印手机尾号是8的学生的名字
name_p8 = [x['name'] for x in students if int(x['tel']) % 10 == 8] print(name_p8)
-
打印最高分和对应的学生的名字
max_score = max([x['score_math'] for x in students]) for x in students: if x['score_math'] == max_score: print(f'最高分为:{x["name"]} {max_score}分')
-
删除性别不明的所有学生
for x in students.copy(): if x['性别'] == '不明': students.remove(x) print(students)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 一 score = [x['score_math'] for x in students] score.sort(reverse=True) print(score) new_ = [] for x in score: for y in students: if y['score_math'] == x: new_.append(y) students = new_ print(students) # 二 students.sort(key=lambda item: item['score_math'], reverse=True) print(students)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class_math = { 'class_name': 'math01', 'class_position': '三栋四楼五教', 'head_teacher': {'name': '大大', 'age': 85, 'tel': '15548574562', 'ed_background': '管理学博士'}, 'lecturer_teacher': {'name': '小小', 'age': 45, 'tel': '15778574552', 'ed_background': '金融学博士'}, 'students':[ {'name': '小明', 'age': 18, 'score_math': 88, 'tel': '18858821154', '性别': '男'}, {'name': '小黄', 'age': 22, 'score_math': 32, 'tel': '15558821158', '性别': '男'}, {'name': '小花', 'age': 17, 'score_math': 75, 'tel': '17758841458', '性别': '女'}, {'name': 'kk', 'age': 97, 'score_math': 99, 'tel': '16258771455', '性别': '不明'}, {'name': 'cc', 'age': 99, 'score_math': 22, 'tel': '12254171447', '性别': '不明'}, {'name': '小白', 'age': 25, 'score_math': 78, 'tel': '11158821141', '性别': '男'}] }
-
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
variety = [x['breed'] for x in dogs] print(variety)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
name_white = [x['name'] for x in dogs if x['color'] == '白色'] print(name_white)
-
给dogs中没有性别的狗添加性别为 ‘公’
for x in dogs: x.setdefault('gender', '公') print(dogs)
-
统计 ‘银狐’ 的数量
count = len([x for x in dogs if x['breed'] == '银狐']) print(count)
-
day8字典作业
最新推荐文章于 2025-05-28 20:48:34 发布