day8字典作业

  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别

    stu_ = {
        'name': '小明', 'age': '18', 'score_math': 88, 'tel': '18858821154', '性别': '男'
    }
    
  2. 定义一个列表,在列表中保存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', '性别': '男'}
    ]
    
    1. 统计不及格学生的个数

      count = 0
      for x in students:
          if x['score_math'] < 60:
              count += 1
      print(count)
      
    2. 打印不及格未成年学生的名字和对应的成绩

      loser = [(x["name"], x["score_math"]) for x in students if x['score_math'] < 60]
      print('不及格学生有:', loser)
      
    3. 求所有男生的平均年龄

      average_man = sum([x['age'] for x in students if x['性别'] == '男']) / len([x['score_math'] for x in students if x['性别'] == '男'])
      print(average_man)
      
    4. 打印手机尾号是8的学生的名字

      name_p8 = [x['name'] for x in students if int(x['tel']) % 10 == 8]
      print(name_p8)
      
    5. 打印最高分和对应的学生的名字

      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}分')
      
    6. 删除性别不明的所有学生

      for x in students.copy():
          if x['性别'] == '不明':
              students.remove(x)
      print(students)
      
    7. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

      # 一
      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)
      
  3. 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)

    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', '性别': '男'}]
    }
    
  4. 已知一个列表保存了多个狗对应的字典:

    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': '母'}
    ]
    
    1. 利用列表推导式获取所有狗的品种

      [‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]

      variety = [x['breed'] for x in dogs]
      print(variety)
      
    2. 利用列表推导式获取所有白色狗的名字

      [‘贝贝’, ‘可乐’]

      name_white = [x['name'] for x in dogs if x['color'] == '白色']
      print(name_white)
      
    3. 给dogs中没有性别的狗添加性别为 ‘公’

      for x in dogs:
          x.setdefault('gender', '公')
      print(dogs)
      
    4. 统计 ‘银狐’ 的数量

      count = len([x for x in dogs if x['breed'] == '银狐'])
      print(count)
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王薇蕴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值