day8-字典和集合作业

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

    list1 = [
        {'name':'吴彦祖','age':'18','score':70,'tel':40508,'sex':'男'},
        {'name':'彭于晏','age':'28','score':30,'tel':41008,'sex':'男'},
        {'name':'成龙','age':'38','score':99,'tel':40999,'sex':'不明'},
        {'name':'林宥嘉','age':'48','score':49,'tel':40081,'sex':'男'},
        {'name':'周杰伦','age':'58','score':80,'tel':40082,'sex':'男'},
        {'name':'吴京','age':'98','score':90,'tel':40084,'sex':'女'}
    ]
    
    1. 统计不及格学生的个数

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

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

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

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

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

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

    
    English = {'stu1','stu2','stu3','stu4','stu9'}
    Math = {'stu2','stu4','stu6','stu7','stu8'}
    Science = {'stu3','stu4','stu5','stu6','stu7','stu2'}
    
    1. 求选课学生总共有多少人

      print('选课总人数为',len(English|Math|Science))
      
    2. 求只选了第一个学科的人的数量和对应的名字

      nums = []
      for i in (English-Math-Science):
          nums.append(i)
      print('学生名字:',nums,'学生数量为',len(English-Math-Science))
      
    3. 求只选了一门学科的学生的数量和对应的名字

      nums = []
      for i in (English - Math - Science):
          for j in (Math - Science - English):
              for k in (Science - English - Math):
                  nums.append(i)
                  nums.append(j)
                  nums.append(k)
                  print(i,j,k,len(nums))
      
    4. 求只选了两门学科的学生的数量和对应的名字

      nums = []
      for i in (English & Math):
          if i not in Science:
              nums.append(i)
      for j in (English & Science):
          if j not in Math:
              nums.append(j)
      for k in (Science & Math):
          if k not in English:
              nums.append(k)
      print('学生名字:',nums,'学生数量为',len(nums))
      
    5. 求选了三门学生的学生的数量和对应的名字

      nums = []
      for i in (English & Math & Science):
          nums.append(i)
      print('学生名字:', nums, '学生数量为', len(English & Math & Science))
      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值