1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student={'姓名':'懒羊羊','年龄':7,'成绩':66,'电话':'18279903567','性别':'男'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students=[
{'姓名':'懒羊羊','年龄':7,'成绩':66,'电话':'18279903567','性别':'男'},
{'姓名':'喜羊羊','年龄':6,'成绩':99,'电话':'18248340271','性别':'男'},
{'姓名':'暖羊羊','年龄':8,'成绩':98,'电话':'18234182931','性别':'女'},
{'姓名':'沸羊羊','年龄':6,'成绩':71,'电话':'18266318351','性别':'男'},
{'姓名':'慢羊羊','年龄':98,'成绩':100,'电话':'18227341041','性别':'男'},
{'姓名':'美羊羊','年龄':7,'成绩':66,'电话':'18279903567','性别':'不明'},
]
a.统计不及格学生的个数
count=0
for person in students:
if person['成绩']<60:
count+=1
print('不及格的人数是%d个'%count)
b.打印不及格学生的名字和对应的成绩
for person in students:
if person['成绩']<60:
print(person['姓名'],person['成绩'])
c.统计未成年学生的个数
count=0
for person in students:
if person['年龄']<18:
count+=1
print('未成年人数%d'%count)
d.打印手机尾号是8的学生的名字
for person in students:
if person['电话'][-1] == '8':
print(person['姓名'])
e.打印最高分和对应的学生的名字
max=0
for person in students:
if person['成绩'] > max:
max=person['成绩']
for p in students:
if p['成绩']==max:
print(p['姓名'])
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for x in range(len(students)):
for j in range(x+1,len(students)):
if students[x]['成绩']<students[j]['成绩']:
students[x],students[j]=students[j],students[x]
for person in students:
print(person)
g.删除性别不明的所有学生
for person in students:
if person['性别']!='男' and person['性别']!='女':
students.remove(person)
for x in students:
print(x)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
math=['美羊羊','喜羊羊','沸羊羊','灰太狼']
chinese=['慢羊羊','灰太狼','红太狼','喜羊羊']
geography=['灰太狼','懒洋洋','暖洋洋','美羊羊']
set1=set(math)|set(chinese)|set(geography)
print(len(set1))
b. 求只选了第一个学科的人的数量和对应的名字
math=['美羊羊','喜羊羊','沸羊羊','灰太狼']
chinese=['慢羊羊','灰太狼','红太狼','喜羊羊']
geography=['灰太狼','懒洋洋','暖洋洋','美羊羊']
set1=set(math)-set(chinese)-set(geography)
print(set1)
c. 求只选了一门学科的学生的数量和对应的名字
math=['美羊羊','喜羊羊','沸羊羊','灰太狼']
chinese=['慢羊羊','灰太狼','红太狼','喜羊羊']
geography=['灰太狼','懒洋洋','暖洋洋','美羊羊']
set1=set(math)&set(chinese)&set(geography)
set2=set(math)^set(chinese)^set(geography)-set1
print(len(set2),set2)
d. 求只选了两门学科的学生的数量和对应的名字
math=['美羊羊','喜羊羊','沸羊羊','灰太狼']
chinese=['慢羊羊','灰太狼','红太狼','喜羊羊']
geography=['灰太狼','懒洋洋','暖洋洋','美羊羊']
set1=set(math)&set(chinese)&set(geography)
set2=set(math)&set(chinese)-set1
set3=set(math)&set(geography)-set1
set4=set(geography)&set(chinese)-set1
set5=list(set2)+list(set3)+list(set4)
print(len(set5),set5)
e. 求选了三门学生的学生的数量和对应的名字
math=['美羊羊','喜羊羊','沸羊羊','灰太狼']
chinese=['慢羊羊','灰太狼','红太狼','喜羊羊']
geography=['灰太狼','懒洋洋','暖洋洋','美羊羊']
set1=set(math)&set(chinese)&set(geography)
print(len(set1),set1)