day6-字典练习

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

student = {
    'name': '大娃',
    'age': 26,
    'scores': 80,
    'tel': '13554784548',
    'sex': '男'

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
​ g.删除性别不明的所有学生

student_information = {
    'student': [{
        'name': '大娃',
        'age': 26,
        'scores': 80,
        'tel': '13554784545',
        'sex': '男'
    },
        {
            'name': '二娃',
            'age': 25,
            'scores': 78,
            'tel': '13532784547',
            'sex': '男'
        },
        {
            'name': '三娃',
            'age': 24,
            'scores': 79,
            'tel': '13554584544',
            'sex': '女'
        },
        {
            'name': '四娃',
            'age': 17,
            'scores': 81,
            'tel': '13554784548',
            'sex': '男'
        },
        {
            'name': '五娃',
            'age': 16,
            'scores': 55,
            'tel': '13553284548',
            'sex': '女'
        },
        {
            'name': '六娃',
            'age': 17,
            'scores': 59,
            'tel': '7355608',
            'sex': '不明'
        },
    ]

}
print("==========a========")
count = 0
for i in student_information['student']:
    if i['scores'] < 60:
        count += 1
print(count)

print("==========b========")
for i in student_information['student']:
    if i['scores'] < 60:
        print("学生%s, 成绩%s" % (i['name'], i['scores']))

print("==========c=========")
count1 = 0
for i in student_information['student']:
    if i['age'] < 18:
        count1 += 1
print(count1)

print("==========d==========")
count2 = 8
for i in student_information['student']:
    if int(i['tel']) % 10 == 8:
        print(i['name'])

print("==========e===========")
max_score = student_information['student'][0]['scores']
for i in student_information['student'][1:]:
    score = i['scores']
    if score > max_score:
        max_score = score
print(max_score)
for x in student_information['student']:
    if x['scores'] == max_score:
        print(x['name'])

print("==========f===========")
student_information['student'].sort(key=lambda item: item['scores'], reverse=True)
print(student_information)

print("===========g============")
for i in student_information['student']:
    if i['sex'] == '不明':
        i.clear()
print(student_information)

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

print("===========3.a========")

student_system = {
    'yuwen': [
        {'name': '李华'},
        {'name': '小明'},
        {'name': '韩梅梅'},
        {'name': '大娃'}
    ],
    'english': [
        {'name': '小明'},
        {'name': '二娃'},
        {'name': '三娃'},
        {'name': '李阳'},
        {'name': '韩梅梅'}

    ],
    'math': [
        {'name': '大娃'},
        {'name': '张三'},
        {'name': '李四'},
        {'name': '王五'}
    ]
}
print("===========3.a========")
count3 = []
for i in student_system['yuwen']:
    count3.append(i['name'])
for j in student_system['english']:
    if j['name'] not in count3:
        count3.append(j['name'])
for x in student_system['math']:
    if x['name'] not in count3:
        count3.append(x['name'])
print(count3)
print(len(count3))

print("===========3.b========")
n = 0
for i in student_system['yuwen']:
    if i not in student_system['english'] and i not in student_system['math']:
        n += 1
        print(n, i['name'])

print("===========3.c========")
list1 = []
list2 = []
list3 = []
list4 = []
for i in student_system['yuwen']:
    list1.append(i['name'])
for i in student_system['math']:
    list2.append(i['name'])
for i in student_system['english']:
    list3.append(i['name'])
count = 0
for i in list1:
    if i not in list2 and i not in list3:
        list4.append(i)
        count += 1
print(count)
for i in list2:
    if i not in list3 and i not in list1:
        list4.append(i)
        count += 1
print(count)
for i in list3:
    if i not in list1 and i not in list2:
        list4.append(i)
        count += 1
print(count, list4)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值