-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student = {'name':'张三','age':18,'score':90, 'tel':110,'gender':'男'}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
- 统计不及格学生的个数
- 打印不及格未成年学生的名字和对应的成绩
- 求所有男生的平均年龄
- 打印手机尾号是8的学生的名字
- 打印最高分和对应的学生的名字
- 删除性别不明的所有学生
- 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students = [{'name':'张三','age':15,'score':90, 'tel':'110','gender':'男'}, {'name': '李四','age': 17, 'score': 59, 'tel': '118', 'gender': '男'}, {'name':'小美','age':16,'score':85, 'tel':'1118','gender':'女'}, {'name':'小丽','age':21,'score':49, 'tel':'120','gender':'女'}, {'name':'王五','age':28,'score':90, 'tel':'130','gender':'男'}, {'name':'奇迹','age':18,'score':86, 'tel':'140','gender':'不明'} ] count = 0 for x in students: if x['score'] < 60: count += 1 print(f'不及格人数{count}') for y in students: names = ((y['name'],y['score'])) if y['score'] < 60 and y['age'] < 18: print(names) total_age = 0 count_man = 0 for c in students: total_age += c['age'] if c['gender'] == '男': count_man += 1 print(total_age // count_man) for a in students: if a['tel'][-1] == '8': print(a['name']) max_score = students[0]['score'] for index in range(1,len(students)): if students[index]['score'] > max_score: max_score = students[index]['score'] print(max_score) for b in students: if b['score'] == max_score: print(b['name'],end='\t') for index1 in range(len(students)): if students[index1]['gender'] == '不明': del students[index1]['gender'] score_list1 = [] for stu in students: score_list1.append(stu['score']) score_list1.sort(reverse=True) new_list =[] index2 = 0 judge = True while judge : for stu in students: if stu['score'] == score_list1[index2]: new_list.append(stu) index += 1 if len(new_list) == len(students): judge = False print(new_list)
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = {
‘name’: ‘Python2202’,
‘address’: ‘14教室’,
‘lecturer’: {‘name’: ‘余婷’, ‘qq’: ‘726550822’, ‘gender’: ‘女’, ‘age’: 18},
‘head_teacher’: {‘name’: ‘舒玲’, ‘tel’: ‘110’, ‘gender’: ‘女’, ‘age’: 18},
‘students’: [
{‘name’: ‘罗浩’, ‘tel’: ‘120’, ‘gender’: ‘男’, ‘major’: ‘软件工程’, ‘age’: 20, ‘linkman’: {‘name’: ‘p1’, ‘tel’: ‘29372’}},
{‘name’: ‘stu1’, ‘tel’: ‘62736’, ‘gender’: ‘女’, ‘major’: ‘电子信息’, ‘age’: 23, ‘linkman’: {‘name’: ‘p2’, ‘tel’: ‘78221’}},
{‘name’: ‘stu2’, ‘tel’: ‘1292831’, ‘gender’: ‘女’, ‘major’: ‘会计’, ‘age’: 27, ‘linkman’: {‘name’: ‘p3’, ‘tel’: ‘28239’}},
{‘name’: ‘stu3’, ‘tel’: ‘72289392’, ‘gender’: ‘男’, ‘major’: ‘数学’, ‘age’: 21, ‘linkman’: {‘name’: ‘p4’, ‘tel’: ‘29102’}},
{‘name’: ‘stu4’, ‘tel’: ‘2737288’, ‘gender’: ‘女’, ‘major’: ‘计算机’, ‘age’: 32, ‘linkman’: {‘name’: ‘p5’, ‘tel’: ‘236721’}},
{‘name’: ‘stu5’, ‘tel’: ‘27361028’, ‘gender’: ‘男’, ‘major’: ‘物理’, ‘age’: 19, ‘linkman’: {‘name’: ‘p6’, ‘tel’: ‘34293’}},
{‘name’: ‘stu6’, ‘tel’: ‘829129’, ‘gender’: ‘男’, ‘major’: ‘石油’, ‘age’: 22, ‘linkman’: {‘name’: ‘p7’, ‘tel’: ‘229311’}}
]
} -
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
print([x['breed'] for x in dogs])
-
利用列表推导式获取所有白色狗的名字
print([x['name'] for x in dogs if x['color'] == '白色'])
-
给dogs中没有性别的狗添加性别为 ‘公’
[x.setdefault('gender','公') for x in dogs ]
-
统计 ‘银狐’ 的数量
list1 = [x for x in dogs if x['breed'] == '银狐'] print(len(list1))
-
day7-字典作业
最新推荐文章于 2023-09-09 10:54:35 发布