-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
students = {"name":"张三","age":18,"score":80,"tel":"123456","gender": "男"}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [ {"name":"张三","age":18,"score":80,"tel":"1234565631","gender": "男"}, {'name':'李四','age':27,'score':77,'tel':'1812727823','gender':'女'}, {'name':'王五','age':20,'score':81,'tel':'1812727828'}, {'name':'陈六','age':16,'score':63,'tel':'1812727888','gender':'男'}, {'name':'吴七','age':22,'score':81,'tel':'18127278289','gender':'女'}, {'name':'孙八','age':15,'score':59,'tel':'18127278091'} ]
-
统计不及格学生的个数
count = 0 for i in students: if i.get("score") < 60: count += 1 print(count)
-
打印不及格未成年学生的名字和对应的成绩
for i in students: if i.get("score") < 60 and i.get("age") < 18: print(i["name"],i["score"])
-
求所有男生的平均年龄
sum_age = count_age = 0 for i in students: if i.get("gender") == "男": sum_age += i["age"] count_age += 1 print(sum_age/count_age)
-
打印手机尾号是8的学生的名字
for i in students: if int(i.get("tel")) % 10 == 8: print(i["name"]) names = [i['name'] for i in students if i['tel'][-1] == '8'] print('4.手机尾号是8的学生的名字:', names) for i in students: if i.get("tel")[-1] == "8": print(i["name"])
-
打印最高分和对应的学生的名字
max_score = max([i["score"] for i in students]) names = [i["name"] for i in students if i["score"] == max_score] print(max_score,names) max_score = students[0]['score'] names = [students[0]['name']] for stu in students[1:]: if stu['score'] > max_score: max_score = stu['score'] names.clear() names.append(stu['name']) elif stu['score'] == max_score: names.append(stu['name']) print('5.2.最高分和对应的学生的名字:', max_score, names)
-
删除性别不明的所有学生
new_student = [i for i in students if i.get("gender")] print(new_student)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students.sort(key=lambda item: item['score'], reverse=True) print(students)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = { 'class_name': 'Python2204', 'address': '15教', 'lecturer': {'name': '余婷', 'age': 18, 'qq': '726550822', 'gender': '女'}, 'class_teacher': {'name': '静静', 'tel': '110'}, 'students': [ {'name': 'stu1', 'age': 21, 'major': '会计', 'tel': '120'}, {'name': 'stu2', 'age': 30, 'major': '电子', 'tel': '219223'}, {'name': 'stu3', 'age': 19, 'major': '旅游管理', 'tel': '123233'}, {'name': 'stu4', 'age': 25, 'major': '通信', 'tel': '4444221'}, {'name': 'stu5', 'age': 25, 'major': '机械', 'tel': '223111'}, {'name': 'stu6', 'age': 23, 'major': '数学', 'tel': '234234'} ] }
-
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
result = [i["breed"] for i in dogs] print(result)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
names = [i["name"] for i in dogs if i["color"] == "白色"] print(names)
-
给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-09-09 10:54:35 发布