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)