-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
stu = {'name': '小明', 'age': 18, 'scores': 100, 'tel':'17867542300', 'gender': '男'} print(stu)S
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
stus = [ {'name': '小明', 'age': 17, 'scores': 100, 'tel': '17867542300', 'gender': '男'}, {'name': '喃喃', 'age': 28, 'scores': 60, 'tel': '965489558', 'gender': '女'}, {'name': '小华', 'age': 18, 'scores': 93, 'tel': '165332300'}, {'name': '小红', 'age': 16, 'scores': 45, 'tel': '1335642300', 'gender': '男'}, {'name': '小李', 'age': 32, 'scores': 73, 'tel': '1064342308'}, {'name': '小王', 'age': 33, 'scores': 58, 'tel': '1177542300', 'gender': '男'} ]
-
统计不及格学生的个数
count = 0 for i in stus: if i['scores'] < 60: count += 1 print(count) # 2
-
打印不及格未成年学生的名字和对应的成绩
for i in stus: if i['scores'] < 60 and i['age'] < 18: print(i['name'], i['scores']) # 小红 45
-
求所有男生的平均年龄
result = [i['age'] for i in stus if i['gender'] == '男'] print(sum(result)/len(result))
-
打印手机尾号是8的学生的名字
for i in stus: if i['tel'][-1] == '8': print(i['name']) # 喃喃 # 小李
-
打印最高分和对应的学生的名字
max1 = stus[0]['scores'] for i in stus[1:]: if i['scores'] > max1: max1 = i['scores'] for i in stus: if i['scores'] == max1: print(i['name'] , max1) # 小明 100
-
删除性别不明的所有学生
stus_1 = stus.copy() for i in stus: if i.get('gender', '不明') == '不明': stus_1.remove(i) print(stus_1)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
new_stus = [] # 存放排序后的列表 # 先取出分数并排序 result = [i['scores'] for i in stus] result.sort(reverse=True) for i in result: for j in stus: if j['scores'] == i: new_stus.append(j) print(new_stus)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = { 'name': 'python2301', 'location': '21教', 'lecturer': { 'name': '余婷', 'age': 18, 'gender': '女', 'tel': '17867453642' }, 'headTeacher': { 'name': '萍姐', 'age': 18, 'tel': '17836237277' }, 'all_student': [ { 'name': '小明', 'age': 18, 'tel': '119', '专业': '电子商务', 'score': 78, 'linkman': {'name': '张三', 'tel': '82223111'} }, { 'name': '小华', 'age': 23, 'tel': '8293', '专业': '计算机软件', 'score': 100, 'linkman': {'name': '李四', 'tel': '829933'} }, { 'name': '小红', 'age': 30, 'tel': '918111', '专业': '数学', 'score': 96, 'linkman': {'name': '王二', 'tel': '11929203'} }, { 'name': '小芳', 'age': 16, 'tel': '827222', '专业': '数学', 'score': 99, 'linkman': {'name': '王五', 'tel': '8628101'} } ] }
-
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
breed = [i['breed'] for i in dogs] print(breed)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
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) # 2
-
day8字典作业
最新推荐文章于 2024-08-11 18:44:16 发布