python -day7-字典作业

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

    stu = {
        'name': '阿七',
        'age': '18',
        'score': {
            'math': 88},
        'tel': '199924',
        'gender': '男'
    }
    
    
  2. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

    stu1 = [
        {'name': '阿七', 'age': 18, 'math': 88, 'tel': '199924', 'gender': '男'},
        {'name': '阿三', 'age': 16, 'math': 82,  'tel': '256898', 'gender': '女'},
        {'name': '阿四', 'age': 19, 'math': 58,  'tel': '198658', 'gender': '女'},
        {'name': '阿九', 'age': 20, 'math': 91, 'tel': '215962', 'gender': '男'},
        {'name': '三七', 'age': 16, 'math': 78,'tel': '115359', 'gender': '不明'},
        {'name': '当归', 'age': 17, 'math': 69,'tel': '199924', 'gender': '女'}
    ]
    
    1. 统计不及格学生的个数

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

      for i in stu1:
          if i['age'] < 18:
              print(i['name'],i['math'])
      
    3. 求所有男生的平均年龄

      total_age = 0
      count = 0
      for i in stu1:
          if i['gender'] == '男':
              total_age += i['age']
              count += 1
      print('男生的平均年龄:', total_age/count)
      
    4. 打印手机尾号是8的学生的名字

      for i in stu1:
          if i['tel'][-1] == '8':
              print(i['name'])
      
    5. 打印最高分和对应的学生的名字

      
      
    6. 删除性别不明的所有学生

      
      or i in stu1:
          if i['gender'] == '不明':
              i .clear()
      print(stu1)
      
    7. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

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

    class1 = {
        'className': 'python2205',
        'classRoom': '11教',
        'lecturer': {'name': '余婷', 'age': 18, 'tel': '13678192302', 'gender': '女'},
        'classTeacher': {'name': '燕子姐', 'age': 18, 'tel': '119', 'QQ': '8282821'},
        ' students': [
            {'name': 'stu1', 'age': 19, 'tel': '11922333', 'gender': '男', 'linkman': {'name': '阿七', 'tel': '14562'}},
            {'name': 'stu2', 'age': 12, 'tel': '11921533', 'gender': '女', 'linkman': {'name': '阿三', 'tel': '18562'}},
            {'name': 'stu3', 'age': 23, 'tel': '11472333', 'gender': '男', 'linkman': {'name': '阿四', 'tel': '16562'}},
            {'name': 'stu4', 'age': 36, 'tel': '11298533', 'gender': '女', 'linkman': {'name': '阿九', 'tel': '18562'}},
            {'name': 'stu5', 'age': 29, 'tel': '11925413', 'gender': '男', 'linkman': {'name': '阿六', 'tel': '15862'}}
        ]
    }
    
  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. 利用列表推导式获取所有狗的品种

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

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

      [‘贝贝’, ‘可乐’]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值