day8-字典和集合作业

本文档概述了如何操作学生信息列表,包括统计不及格学生数量、筛选特定条件的学生、课程选课情况分析,以及使用集合表示选课情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

    name = {
        'stu':[
            {'name':'stu1','age':18,'score':80,'tel':'123','gender':'男'},
            {'name':'stu2','age':20,'score':74,'tel':'134','gender':'女'},
            {'name':'stu3','age':19,'score':45,'tel':'148','gender':'男'},
            {'name':'stu4','age':22,'score':90,'tel':'156','gender':'不明'},
            {'name':'stu5','age':24,'score':56,'tel':'173','gender':'女'},
            {'name':'stu6','age':21,'score':60,'tel':'192','gender':'男'}
        ]
    }
    
    1. 统计不及格学生的个数

      count = 0
      all_stu = name['stu']
      for stu in all_stu:
          if stu['score'] < 60:
              count += 1
      print('不及格人数:',count)
      
    2. 打印不及格学生的名字和对应的成绩

      all_stu = name['stu']
      for stu in all_stu:
          if stu['score'] < 60:
              print(stu['name'])
      
    3. 打印手机尾号是8的学生的名字

      all_stu = name['stu']
      for stu in all_stu:
          if int(stu['tel']) % 10 ==  8:
              print(stu['name'])
      
    4. 打印最高分和对应的学生的名字

      Max_Score = name[0]['score'] 
      for x in name[1:]:
          score = x['score']
          if score > Max_Score:
              Max_Score = score
      for x in name:
          if x['score'] == Max_Score:
              print(Max_Score, ':', x['name'])
      
    5. 删除性别不明的所有学生

      for stu in name:
          if stu['gender'] == '不明':
              name.remove(stu)
      print(name)
      
    6. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

      
      
  2. 用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)

   chinese = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
   math = ['C', 'E', 'F', 'J', 'K', 'L', 'I']
   english = ['A', 'B', 'C', 'E', 'M', 'N']
  1. 求选课学生总共有多少人

       print(len(list(dict.fromkeys(chinese + math + english))))
    
    1. 求只选了第一个学科的人的数量和对应的名字

      combine = chinese + math + english
      count = 0
      print('只选了第一门学科的有:', end='')
      for x in chinese:
          if x not in math and x not in english:
              print(x, end=' ')
              count += 1
      print(' 总共有%d人' % count)
      
    2. 求只选了一门学科的学生的数量和对应的名字

      
      
    3. 求只选了两门学科的学生的数量和对应的名字

      
      
    4. 求选了三门学生的学生的数量和对应的名字

      all_stu = []
      for x in list(dict.fromkeys(chinese + math + english)):
          if x in chinese and x in math and x in english:
              all_stu.append(x)
      print('三门都选了的人数:', len(all_stu), '   对应的学生是:', all_stu, )
      
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值