day6作业

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

stu_information = {"name": "张三", "sex": "man", "age": 21, "tel": 138138, "grade": 81}

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)

stu_information = {
    "students": [
        {"name": "张三", "sex": "男", "age": 21, "tel": 138138, "grade": 81},
        {"name": "李四", "sex": "男", "age": 22, "tel": 137137, "grade": 75},
        {"name": "王五", "sex": "男", "age": 23, "tel": 136136, "grade": 59},
        {"name": "小明", "sex": "不明", "age": 20, "tel": 135135, "grade": 69},
        {"name": "小丽", "sex": "女", "age": 19, "tel": 134134, "grade": 45},
        {"name": "小美", "sex": "女", "age": 20, "tel": 133133, "grade": 89}
    ]
}

​ a.统计不及格学生的个数

count = 0
for i in stu_information["students"]:
    if i["grade"] < 60:
        count += 1
print(count)

​ b.打印不及格学生的名字和对应的成绩

for i in stu_information["students"]:
    if i["grade"] < 60:
        print(i["name"], i["grade"])

​ c.统计未成年学生的个数

count = 0
for i in stu_information["students"]:
    if i["age"] < 18:
        count += 1
print(count)

​ d.打印手机尾号是8的学生的名字

for i in stu_information["students"]:
    if i["tel"] % 10 == 8:
        print(i["name"])

​ e.打印最高分和对应的学生的名字

max_grade = 0
for i in stu_information["students"]:
    if i["grade"] > max_grade:
        name = i["name"]
        max_grade = i["grade"]
print(max_grade, name)

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

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

for i in stu_information["students"]:
    if i["sex"] == "不明":
        print(i)
        stu_information["students"].remove(i)
print(stu_information)

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

list1 = [1, 2, 3, 4, 5]
list2 = [2, 4, 6, 7, 8]
list3 = [1, 3, 5, 7, 9]

​ a. 求选课学生总共有多少人

a = []
for i in list1:
    a.append(i)
for j in list2:
    if j not in list1 and j not in a:
        a.append(j)
for h in list3:
    if h not in (list1 + list2) and h not in a:
        a.append(h)
print(len(a))
# 方法二
a = set(list3 + list2 + list1)
print(len(a))
b. 求只选了第一个学科的人的数量和对应的名字
a = []
for i in list1:
    if i not in (list2 + list3):
        a.append(i)
print(len(a), a)

​ c. 求只选了一门学科的学生的数量和对应的名字

a = []
for i in list1:
    for j in list2:
        for h in list3:
            if i not in (list2 + list3) and i not in a:
                a.append(i)
            if j not in (list1 + list3) and j not in a:
                a.append(j)
            if h not in (list1 + list2) and h not in a:
                a.append(h)
print(a)
d. 求只选了两门学科的学生的数量和对应的名字
a = set(list3 + list2 + list1)
b = []
for i in a:
    if (i in list1) and (i in list2) and (i not in list3) or \
            (i in list2) and (i in list3) and (i not in list1) or \
            (i in list3) and (i in list1) and (i not in list2):
        b.append(i)
print(len(b), b)

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

a = set(list3 + list2 + list1)
b = []
for i in a:
    if (i in list1) and (i in list2) and (i in list3):
        b.append(i)
print(len(b), b)
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值