day7字典和集合的应用练习

1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)

student = {'name': 'Tommy', 'age': 18, 'score': 60, 'tel': '123456', 'sex': 'male'}

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字

​ f.删除性别不明的所有学生

​ g.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

students = [
    {'name': '张三', 'age': 18, 'score': 60, 'tel': '12347556787', 'sex': 'male'},
    {'name': '李四', 'age': 24, 'score': 58, 'tel': '12345611238', 'sex': 'female'},
    {'name': '王五', 'age': 22, 'score': 77, 'tel': '12345533356', 'sex': 'emale'},
    {'name': '田七', 'age': 17, 'score': 87, 'tel': '12345612383', 'sex': 'female'},
    {'name': '狗蛋', 'age': 19, 'score': 12, 'tel': '12345635338', 'sex': 'male'},
    {'name': '小铭', 'age': 23, 'score': 57, 'tel': '12345631645', 'sex': 'female'},
]
count_score = 0  # 记录不及格的人数
count_age = 0  # 记录不及格的人数
Max = students[0]['score']
# Max_name = students[0]['name']
for student in students:
    if student['score'] < 60:
        count_score += 1
        print('不及格的:', student['name'], student['score'])  # 打印不及格的人的姓名和成绩
    if student['age'] < 18:
        count_age += 1
    if Max < student['score']:
        Max = student['score']
       # Max_name = student['name']  # 此种写法当最大值有多个时:只会取第一个
    if student['tel'].endswith('8'):
        print('手机号尾数是8的:', student['name'])

print('不及格的人数为:', count_score)
print('未成年的人数为:', count_age)
Max_name = [student['name'] for student in students if student['score'] == Max]  # 把分数最高的人的姓名保存起来
print('最高分是:', Max, '最高分的名字是:', Max_name)
for student in students[:]:  # 删除不男不女
    if (student['sex'] != 'male') and (student['sex'] != 'female'):
        students.remove(student)
for i in range(len(students)):
    for j in range(i + 1, len(students)):   # 排序 
        if students[i]['score'] < students[j]['score']:
            students[i], students[j] = students[j], students[i]
print(students)

3.用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字

sing = ('张三', '李四', '王五', '麻子', '熊大', '牛二', '太白', '太黑')
dance = ('李四', '王五', '麻子', '狗蛋', '田七')
rap = ('张三', '熊大', '王五', '牛二', '狗蛋', '月中眠')
# 法一: 利用列表推导式
# (1)一共有多少人
all_people = set(sing + dance + rap)
print('一共有多少:', '人', len(all_people), all_people, sep='')
# (2)只选了第一门课的人
print('只选了第一门课的人:', [x for x in sing if x not in dance and x not in rap])
# (3)只选了一门课的人
print('只选了一门课的人:', [x for x in all_people if (x in sing and x not in dance and x not in rap) \
                    or (x not in sing and x in dance and x not in rap) \
                    or (x not in sing and x not in dance and x in rap)])
# (4)只选了两门课的人
print('只选了两门课的人:', [x for x in all_people if (x in sing and x in dance and x not in rap) \
                    or (x in sing and x not in dance and x in rap) \
                    or (x not in sing and x in dance and x in rap)])
# (5)三门课都选了的人
print('三门课都选了的人:', [x for x in all_people if x in sing and x in dance and x in rap])


# 法二: 运用字典的键值对解题 
new_dict = {}
for name in sing + dance + rap:
    if name not in new_dict:
        new_dict[name] = (sing + dance + rap).count(name)
print(new_dict.items())
for k, v in new_dict.items():
    if v == 1:
        print('只选了一门课的学生名字叫' + k)
    elif v == 2:
        print('选了两门课的学生名字叫' + k)
    elif v == 3:
        print('选了三门课的学生名字叫' + k)

# 法三: 运用集合的方法解题

# a. 求选课学生总共有多少人
print('总人数为:', sing | dance | rap)
# b. 求只选了第一个学科的人的数量和对应的名字
print('只选了第一个学科的人的数量为', len(sing - (dance | rap)), '对应的名字:', sing - (dance | rap))
# c. 求只选了一门学科的学生的数量和对应的名字
one = (sing - (dance | rap)) | (dance - (sing | rap)) | (rap - (dance | sing))
print('只选了一个学科的人的数量为', len(one), '对应的名字:', one)
# d. 求只选了两门学科的学生的数量和对应的名字
two = (sing & dance - rap) | (rap & dance - sing) | (sing & rap - dance)
print('只选了两门学科的人的数量为', len(two), '对应的名字:', two)
# e. 求选了三门学生的学生的数量和对应的名字
print('选了三门学生的学生的数量:', len(sing & dance & rap), '对应的名字:', sing & dance & rap)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值