day7-字典作业

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

    students = [{'name': '张三', 'age': 23, 'score': 62, 'cell': '110', 'gender': '不明'},
                {'name': '李四', 'age': 24, 'score': 70, 'cell': '120', 'gender': '不明'},
                {'name': '王二', 'age': 22, 'score': 98, 'cell': '119', 'gender': '男'},
                {'name': '二狗', 'age': 2, 'score': 42, 'cell': '114', 'gender': '不明'},
                {'name': '小翠', 'age': 18, 'score': 95, 'cell': '118', 'gender': '不明'},
                {'name': '小花', 'age': 12, 'score': 8, 'cell': '1168', 'gender': '女'}]
    
    1. 统计不及格学生的个数

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

      for x in students:
          if x['score'] < 60:
              print('不及格学生姓名:', x['name'], ';分数:', x['score'], sep='')
      
    3. 统计未成年学生的个数

      count1 = 0
      for x in students:
          if x['age'] < 18:
              count1 += 1
      print('未成年学生的个数:', count1)
      
    4. 打印手机尾号是8的学生的名字

      print('手机尾号是8的学生有:', end='')
      for x in students:
          if int(x['cell']) % 10 == 8:
              print(x['name'], sep='', end=' ')
      print()
      
    5. 打印最高分和对应的学生的名字

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

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

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

    Math = ('张三', '李四')
    English = ('张三', '王二')
    Music = ('张三', '李四', '王二')
    subjects = [Math, English, Music]
    
    1. 求选课学生总共有多少人

      list1 = []
      for x in subjects:
          for y in x[0:]:
              if y not in list1:
                  list1.append(y)
      print('选课学生共有:', len(list1))
      
    2. 求只选了第一个学科的人的数量和对应的名字

      print('只选了第一个学科的人数:', len(subjects[0]))
      for x in subjects[0][:]:
          print(x)
      
    3. 求只选了一门学科的学生的数量和对应的名字

      list1 = []
      count1 = 0
      for x in subjects[0]:
          if x not in subjects[1] and x not in subjects[2]:
              list1.append(x)
              count1 += 1
      for y in subjects[1]:
          if y not in subjects[0] and y not in subjects[2]:
              list1.append(y)
              count1 += 1
      for z in subjects[2]:
          if z not in subjects[0] and z not in subjects[1]:
              list1.append(z)
              count1 += 1
      print('只选了一门学科的学生人数:', count1, ';名字为:', list1)
      
    4. 求只选了两门学科的学生的数量和对应的名字

      list1 = []
      count1 = 0
      for x in subjects[0]:
          if x in subjects[1] and x not in subjects[2]:
              list1.append(x)
              count1 += 1
      for y in subjects[1]:
          if y not in subjects[0] and y in subjects[2]:
              list1.append(y)
              count1 += 1
      for z in subjects[2]:
          if z in subjects[0] and z not in subjects[1]:
              list1.append(z)
              count1 += 1
      print('只选了两门学科的学生人数:', count1, ';名字为:', list1)
      
    5. 求选了三门学生的学生的数量和对应的名字

      list1 = []
      count1 = 0
      for x in subjects[0]:
          if x in subjects[1] and x in subjects[2]:
              list1.append(x)
              count1 += 1
      print('选了三门学科的学生人数:', count1, ';名字为:', list1)
      
上一篇:Python笔记


下一篇:python练习题1