# 1.声明一个字典保存一个学生的信息,
# 学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
# 2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
stu1={"name":"Tom","tel":"13838498786","age":17,"score_English":99,"gender":"man"}
print(stu1)
stu_list=[{"name":"Tom","tel":"13838498786","age":15,"score_English":90,"gender":"man"},
{"name":"Jimmy","tel":"1388498837","age":17,"score_English":51,"gender":"man"},
{"name":"Andy","tel":"13838498268","age":19,"score_English":75,"gender":"unknown"},
{"name":"John","tel":"13838498723","age":20,"score_English":56,"gender":"man"},
{"name":"Tim","tel":"138384987818","age":19,"score_English":98,"gender":"unknown"},
{"name":"Dawn","tel":"13838498956","age":16,"score_English":99,"gender":"woman"}]
sum_fail=0
sum_nonage=0
tel_num=[]
max_score=0
score_list=[]
# a.统计不及格学生的个数
# b.打印不及格学生的名字和对应的成绩
for stu in stu_list:
if stu["score_English"]<60:
sum_fail+=1
print("不及格的学生有:%s,其分数是%d" % (stu["name"],stu["score_English"]))
# c.统计未成年学生的个数
if stu["age"]<18:
sum_nonage+=1
# d.打印手机尾号是8的学生的名字
if int(stu["tel"])%10==8:
tel_num.append(stu["name"])
# e.打印最高分和对应的学生的名字
if stu["score_English"]>=max_score:
max_score=stu["score_English"]
# f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
score_list.append(stu["score_English"])
score_list.sort(reverse=True)
new_stu_list=[]
for i in score_list:
for stu in stu_list[:]:
if stu["score_English"]==i:
new_stu_list.append(stu)
stu_list.pop(stu)
print("不及格学生的个数是%d;未成年学生的个数是:%d"%(sum_fail,sum_nonage))
print("手机尾号为8的学生有:%s"%tel_num)
print("%s得到最高分为:%d"%(stu["name"],max_score))
print(new_stu_list)
print("====================================================")
#g.删除性别不明的所有学生
for index in range(len(stu_list)-1,-1,-1):
if stu_list[index]["gender"]=="unknown":
stu_list.pop(index)
print(stu_list)
# 3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese_set={"张三","李四","王五","王麻子","詹姆斯","库里","汤普森"}
english_set={"王麻子","詹姆斯","库里","利拉德","比尔","沃尔"}
math_set={"王麻子","卡哇伊","库里","字母哥","沃尔","哈登","乔治"}
# a. 求选课学生总共有多少人
total_stu=chinese_set|english_set|math_set
total_stu_num=0
for _ in total_stu:
total_stu_num+=1
print("总共的选课人数是:%d"%total_stu_num)
# b. 求只选了第一个学科的人的数量和对应的名字
select_1st_num=0
for _ in chinese_set:
select_1st_num+=1
print("只选了第一门课的学生总数是:%s"%select_1st_num,chinese_set)
# c. 求只选了一门学科的学生的数量和对应的名字
select_1=list(chinese_set)+list(english_set)+list(math_set)
new_select_1=[]
for i in select_1:
if select_1.count(i) == 1:
new_select_1.append(i)
print("选择两门课的学生总数是:%d"%len(list(set(new_select_1))),set(new_select_1))
# d. 求只选了两门学科的学生的数量和对应的名字
select_2=list(chinese_set)+list(english_set)+list(math_set)
new_select_2=[]
for i in select_2:
if select_2.count(i)==2:
new_select_2.append(i)
print("选择两门课的学生总数是:%d"%len(list(set(new_select_2))),set(new_select_2))
# e. 求选了三门学生的学生的数量和对应的名字
select_3=chinese_set&english_set&math_set
sum3_stu=0
for _ in select_3:
sum3_stu+=1
print("选择3门课的学生总数为%d"%sum3_stu,select_3)