day7-字典作业

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

    students = [
        {
            'name': '小花',
            'age': 17,
            'score': 87,
            'tel': '182*****2345',
            'gender': '女'
        },
        {
            'name': '吴明',
            'age': 22,
            'score': 98,
            'tel': '134*****6788',
            'gender': '男'
        },
        {
            'name': '小强',
            'age': 23,
            'score': 56,
            'tel': '145*****2367',
            'gender': '不明'
        },
        {
            'name': '碧儿',
            'age': 21,
            'score': 99,
            'tel': '139*****6758',
            'gender': '女'
        },
        {
            'name': '张小小',
            'age': 19,
            'score': 78,
            'tel': '123*****4589',
            'gender': '不明'
        },
        {
            'name': '赵小苏',
            'age': 25,
            'score': 45,
            'tel': '145*****3467',
            'gender': '男'
        }
    ]
    
    1. 统计不及格学生的个数
    count = 0
    for x in students:
        if x.get('score') < 60:
            count += 1
    print('不及格学生的个数:', count)
    
    1. 打印不及格学生的名字和对应的成绩
    print('不及格的学生名字和对应的成绩')
    for x in students:
        if x.get('score') < 60:
            print(x['name'], x['score'])
    
    1. 统计未成年学生的个数
    count = 0
    for x in students:
        if x['age'] < 18:
            count += 1
    print('未成年学生的个数:', count)
    
    1. 打印手机尾号是8的学生的名字
    for x in students:
        if x['tel'][-1] == '8':
            print('手机尾号是8的学生为:', x['name'])
    
    1. 打印最高分和对应的学生的名字

# 方法1
# 第一次循环获取最高分
score_max = students[0]['score']
score_max_name = students[0]['name']
for x in students[1:]:
  if score_max < x['score']:
      score_max = x['score']
      score_max_name = x['name']
print('最高分:', score_max, '学生姓名为:', score_max_name)
#第二次循环找到分数和最高峰相等的所有学生姓名
for stu in students:
  if stu['score'] == score_max:
      print('学生姓名为:', stu['name'])
print('--------------------')
#方法2
score_max = students[0]['score']   # 假设第一个学生的分数最高
names = [students[0]['name']]   # 假设第一个学生的分数最高,保存第一个学生的姓名
for stu in students[1:]:
  score = stu['score']
  if score == score_max:
      names.append(stu['name'])
  elif score > score_max:
      names.clear()
      names.append(stu['name'])
      score_max = score
print('最高分:', score_max, '学生姓名为:', names)
  1. 删除性别不明的所有学生
# 方法1
for stu in students[:]:
 if stu['gender'] == '不明':
     students.remove(stu)
print(students)

#方法2
list1 = [stu for stu in students if stu['gender'] != '不明']
print(list1)
  1. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 方法1
for x in range(len(students)):
    for y in range(len(students)-x-1):
        if students[y]['score'] < students[y+1]['score']:
            mmm = students[y+1]
            students[y+1] = students[y]
            students[y] = mmm
print(students)
# 方法2
students.sort(key=lambda item: item['score'], reverse=True)
print(students)
  1. 用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课)

    science = ('小花', '小明')
    art = ('小王', '张三', '李四')
    sing = ('小王', '张三', '李四', '小碧', '小芳')
    
    1. 求选课学生总共有多少人
    list1 = list(science) + list(art) + list(sing)
    list2 = []
    for x in list1:
        if x not in list2:
            list2.append(x)
    print('选课学生总共有:', len(list2))
    
    1. 求只选了第一个学科的人的数量和对应的名字
    count = 0
    for x in science:
        if x not in art and x not in sing:
            count += 1
            print(x, end=' ')
    print()
    print('只选了第一个学科的人的数量:', count)
    
    1. 求只选了一门学科的学生的数量和对应的名字
    count = 0
    list1 = list(science) + list(art) + list(sing)
    for x in list1:
        if list1.count(x) == 1:
            count += 1
            print(x, end=' ')
    print()
    print('只选了一门学科的学生的数量:', count)
    
    1. 求只选了两门学科的学生的数量和对应的名字
    list1 = list(science) + list(art) + list(sing)
    list2 = []
    for x in list1:
        if list1.count(x) == 2 and x not in list2:
            list2.append(x)
    for y in list2:
        print(y, end=' ')
    print()
    print('只选了两门学科的学生的数量:', len(list2))
    

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

    list1 = list(science) + list(art) + list(sing)
    list3 = []
    for x in list1:
        if list1.count(x) == 3 and x not in list3:
            list3.append(x)
    for y in list3:
        print(y, end=' ')
    print()
    print('选了三门学生的学生的数量:', len(list3))
    
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值