"""_ author _=陈泓羽"""
1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
print("======================第1题============================")
student_dict = {"姓名":"光头强","年龄":18,"成绩(单科)":100,"电话":"12345678912"}
print(student_dict)
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
student_list=[
{"姓名":"光头强","年龄":18,"成绩(单科)":100,"电话":"13045678912"},
{"姓名":"熊大","年龄":17,"成绩(单科)":45,"电话":"1311321323"},
{"姓名":"熊二","年龄":16,"成绩(单科)":98,"电话":"13221321338"},
{"姓名":"熊三","年龄":20,"成绩(单科)":59,"电话":"13345678912"},
{"姓名":"熊四","年龄":14,"成绩(单科)":96,"电话":"13445678912"},
{"姓名":"熊五","年龄":13,"成绩(单科)":95,"电话":"13545678918"}
]
a.统计不及格学生的个数
print("=============问题a============")
count1 = 0
for student_dict in student_list:
if student_dict["成绩(单科)"] < 60:
count1 += 1
print(count1)
b.打印不及格学生的名字和对应的成绩
print("=============问题b============")
for student_dict in student_list:
if student_dict["成绩(单科)"] < 60:
print(student_dict["姓名"],student_dict["成绩(单科)"])
c.统计未成年学生的个数
print("=============问题c============")
count2 = 0
for student_dict in student_list:
if student_dict["年龄"] < 18:
count2 += 1
print(count2)
d.打印手机尾号是8的学生的名字
print("=============问题d============")
for student_dict in student_list:
if (student_dict["电话"])[-1] == "8":
print(student_dict["姓名"])
e.打印最高分和对应的学生的名字
print("=============问题e============")
str_nums = []
for student_dict in student_list:
str_nums.append(student_dict["成绩(单科)"])
print(max(str_nums))
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
print("=============问题f============")
str_nums1 = []
str_nums2 = []
for student_dict in student_list:
str_nums1.append(student_dict["成绩(单科)"])
str_nums1.sort(reverse = True)
for index in range(len(str_nums1)):
for student_dict in student_list:
if str_nums1[index] == student_dict["成绩(单科)"]:
str_nums2.append(student_dict)
print(str_nums2)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list2 = ["李四","王二","李青","小小"]
list3 = ["王二","麻子","伊泽瑞尔","花花"]
a. 求选课学生总共有多少人
print("=============问题a============")
count1 = 0
set1= set(list1)
set2= set(list2)
set3= set(list3)
new_set1 = set1|set2|set3
for x in new_set1:
count1 += 1
print("选课学生总共有:",count1,"人")
b. 求只选了第一个学科的人的数量和对应的名字
print("=============问题b============")
new_set2 = set1 - (set2|set3)
print(len(new_set2),new_set2)
c. 求只选了一门学科的学生的数量和对应的名字
print("=============问题c============")
new_set3 = set2 - (set1|set3)
new_set4 = set3 - (set1|set2)
new_set5 = new_set2|new_set3|new_set4
print(len(new_set5),new_set5)
d. 求只选了两门学科的学生的数量和对应的名字
print("=============问题d============")
new_set6 = set1 & set2 - set3
new_set7 = set1 & set3 - set2
new_set8 = set2 & set3 - set1
new_set9 = new_set6|new_set7|new_set8
print(len(new_set9),new_set9)
e. 求选了三门学生的学生的数量和对应的名字
print("=============问题e============")
new_set10 = set1 & set2 & set3
print(len(new_set10),new_set10)