day7-字典作业

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

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

    stu=[
        {'name': '张三', 'age': 18, 'score': 92, 'tel': '13011110000', 'gender': '男'},
        {'name': '李四', 'age': 19, 'score': 63, 'tel': '13011112222', 'gender': ' 不明'},
        {'name': '王麻子', 'age': 20, 'score': 59, 'tel': '13011113333', 'gender': '男'},
        {'name': '李华', 'age': 17, 'score': 80, 'tel': '13011114444', 'gender': '女'},
        {'name': '小明', 'age': 19, 'score': 95, 'tel': '13011115555', 'gender': '不明'},
        {'name': '小红', 'age': 21, 'score': 45, 'tel': '13011116666', 'gender': '女'}
    ]
    
    1. 统计不及格学生的个数

      count1=0
      for  i in stu:
          if i['score']<60:
              count1+=1
      print(f'不及格学生人数:{count1}人')
      
    2. 打印不及格未成年学生的名字和对应的成绩

      print([[i['name'],i['score']] for i in stu if i['age']<18])
      
      [['王麻子', 59], ['李华', 80]]
      
    3. 求所有男生的平均年龄

      count1=sum1=0
      for i in stu:
          if i['gender']=='男':
              count1+=1
              sum1+=i['age']
      print(f'平均年龄:{sum1//count1}')
      
      平均年龄:17
      
    4. 打印手机尾号是8的学生的名字

      print([i['name'] for i in stu if i['tel'][-1]=='8'])
      
      ['张三', '李华', '小红']
      
    5. 打印最高分和对应的学生的名字

      max1=0
      for i in stu:
          if max1<i['score']:
              max1=i['score']
      # print(max1)
      for j in stu:
          if max1 == j['score']:
              print(j['name'],j['score'])
      
    6. 删除性别不明的所有学生

      for i in range(len(stu)-1,-1,-1):
          if stu[i]['gender']=='不明':
              stu.pop(i)
      print(stu)
      
      for i in stu[:]:
          if i['gender']=='不明':
              stu.remove(i)
      print(stu)
      
      list1=[]
      for i in stu:
          if i['gender']!='不明':
              list1.append(i)
      print(list1)
      
      i=0
      while True:
          if stu[i]['gender']=='不明':
              stu.pop(i)
          else:
              i+=1
          if i==len(stu):
              break
      print(stu)
      
    7. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

      for  i in range(len(stu)):
          for j in range(len(stu)-1):
              if stu[j]['score']<stu[j+1]['score']:
                  stu[j],stu[j+1]=stu[j+1],stu[j]
      print(stu)
      
    for i in range(len(stu)):
        for j in range(i,len(stu)):
            if stu[i]['score']<stu[j]['score']:
                stu[i], stu[j] = stu[j], stu[i]
    print(stu)
    
    for i in range(len(stu)):
        for j in range(i,-1,-1):
            if stu[j]['score']>stu[j-1]['score']:
                stu[j-1],stu[j]=stu[j],stu[j-1]
            else:
                break
    print(stu)
    
    for i in range(len(stu)):
        max1 = i
        for j in range(i,len(stu)-1):
            if stu[j]['score']>stu[i]['score']:
                max1=j
            if max1!= i:
                stu[max1],stu[i]=stu[i],stu[max1]
    print(stu)
    
  3. 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)

    class1 = {
        'className': 'Python2205',
        'classRoom': '11教室',
        'classTeacher': {'name': '燕子姐', 'age': 18, 'tel': '119', 'QQ': '8282821'},
        'lecturer': {'name': '婷姐', 'age': 18, 'tel': '13678192302', 'gender': '女'},
        'students': [
            {'name': '刘毅', 'age': 18, 'tel': '13829394774', 'gender': '男', 'linkman': {'name': '张三', 'tel': '11923'}},
            {'name': 'stu2', 'age': 28, 'tel': '8727832', 'gender': '男', 'linkman': {'name': '小明', 'tel': '19202'}},
            {'name': 'stu3', 'age': 30, 'tel': '291912', 'gender': '女', 'linkman': {'name': '李四', 'tel': '1910283'}},
            {'name': 'stu4', 'age': 17, 'tel': '2349183', 'gender': '男', 'linkman': {'name': '老王', 'tel': '109221567'}},
            {'name': 'stu5', 'age': 16, 'tel': '27273482', 'gender': '女', 'linkman': {'name': '王五', 'tel': '119283427'}},
            {'name': 'stu6', 'age': 22, 'tel': '1242402', 'gender': '男', 'linkman': {'name': '赵六', 'tel': '2091834'}}
        ]
    }
    
  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. 利用列表推导式获取所有狗的品种

      print([i['breed'] for i in dogs])
      

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

    2. 利用列表推导式获取所有白色狗的名字

      print([i['name'] for i in dogs if i['color']=='白色'])
      

      [‘贝贝’, ‘可乐’]

    3. 给dogs中没有性别的狗添加性别为 ‘公’

      for i in dogs:
          i.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、付费专栏及课程。

余额充值