-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
stu = {'name': 'lili', 'age': 18, 'scores': 90, 'tel': '123456', 'gender': '女'}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
- 统计不及格学生的个数
- 打印不及格未成年学生的名字和对应的成绩
- 求所有男生的平均年龄
- 打印手机尾号是8的学生的名字
- 打印最高分和对应的学生的名字
- 删除性别不明的所有学生
- 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
info = [ {'name': 'stu1', 'age': 18, 'scores': 90, 'tel': '1234568', 'gender': '女'}, {'name': 'stu2', 'age': 21, 'scores': 67, 'tel': '124351', 'gender': '男'}, {'name': 'stu3', 'age': 17, 'scores': 98, 'tel': '143565', 'gender': '女'}, {'name': 'stu4', 'age': 19, 'scores': 58, 'tel': '1276898', 'gender': '男'}, {'name': 'stu5', 'age': 18, 'scores': 88, 'tel': '1786596', 'gender': ''}, {'name': 'stu6', 'age': 17, 'scores': 48, 'tel': '1765492', 'gender': '女'} ] # 2-1 统计不及格学生的个数 count = 0 for i in info: if i['scores'] < 60: count += 1 print(count) # 2 # 2-2打印不及格未成年学生的名字和对应的成绩 for i in info: if i['scores'] < 60 and i['age'] < 18: print(i['name'], i['scores']) # stu6 48 # 2-3 求所有男生的平均年龄 age=[i['age'] for i in info if i['gender']=='男'] print(round(sum(age)/len(age),2))#19.33 # 2-4打印手机尾号是8的学生的名字 name=[i['name'] for i in info if i['tel'][-1]=='8'] print(name) # ['stu1', 'stu4'] # 2-5 打印最高分和对应的学生的名字 max_score = info[0]['scores'] names = [info[0]['name']] for i in info[1:]: if i['scores'] > max_score: max_score = i['scores'] names.clear() names.append(i['name']) elif i['scores'] == max_score: names.append(i['name']) print(names, max_score) # 解法2 print('----------------华丽的分割线----------------') max_score = max([i['scores'] for i in info]) names = [i['name'] for i in info if i['scores'] == max_score] print(names, max_score) # 2-6 删除性别不明的所有学生 for i in info[-1::-1]: if not (i['gender'] == '女' or i['gender'] == '男'): info.remove(i) print(info) # 2-7 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃) for i in range(len(info)-1,-1,-1): for j in range(len(info)-1,i,-1): if info[j]['scores']>info[j-1]['scores']: info.insert(j-1,info[j]) del info[j+1] print(info) # 解法2 info.sort(key=lambda item:item['scores'],reverse=True) print(info)
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
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', 'contacts': {'name': '张三', 'tel': '162723'}}, {'name': 'stu2', 'age': 30, 'major': '电子', 'tel': '219223', 'contacts': {'name': '小明', 'tel': '281912'}}, {'name': 'stu3', 'age': 19, 'major': '旅游管理', 'tel': '123233', 'contacts': {'name': '小花', 'tel': '886552'}}, {'name': 'stu4', 'age': 25, 'major': '通信', 'tel': '4444221', 'contacts': {'name': '李四', 'tel': '22342345'}}, {'name': 'stu5', 'age': 25, 'major': '机械', 'tel': '223111', 'contacts': {'name': '王五', 'tel': '555632'}}, {'name': 'stu6', 'age': 23, 'major': '数学', 'tel': '234234', 'contacts': {'name': '赵六', 'tel': '96533'}} ] }
-
已知一个列表保存了多个狗对应的字典:
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中没有性别的狗添加性别为 ‘公’
-
统计 ‘银狐’ 的数量
# 4-1 breed = [i['breed'] for i in dogs] print(breed) # ['银狐', '法斗', '土狗', '哈士奇', '银狐', '土狗'] # 4-2 dogs_name = [i['name'] for i in dogs if i['color'] == '白色'] print(dogs_name) # ['贝贝', '可乐'] # 4-3 for i in dogs: i.setdefault('gender','公') print(dogs) # 4-4 count=0 for i in dogs: if i['breed']=='银狐': count+=1 print(count) # 2
-
005字典练习题
于 2022-07-27 18:38:30 首次发布