"""------ author == 李 墨 ------"""
1
声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
student_1={'name':'张三','age':18,'result':80,'phonenum':18012345678}
print(student_1)
2
声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
student_1={'name':'张三','age':18,'result':80,'phonenum':'18012345678'}
student_2={'name':'李四','age':19,'result':60,'phonenum':'18012343373'}
student_3={'name':'王五','age':17,'result':50,'phonenum':'12012345678'}
student_4={'name':'大黄','age':20,'result':70,'phonenum':'13012343376'}
student_5={'name':'赵四','age':16,'result':40,'phonenum':'14012345671'}
student_6={'name':'周一','age':17,'result':30,'phonenum':'15012343378'}
list_students=[student_1,student_2,student_3,student_4,student_5,student_6]
a
统计不及格学生的个数
count_60=0
for item in list_students:
if item['result'] < 60:
count_60+=1
print(count_60)
b
打印不及格学生的名字和对应的成绩
for item in list_students:
if item['result'] < 60:
print(item['name'],item['result'])
c
统计未成年学生的个数
count_young=0
for item in list_students:
if item['age'] < 18:
count_young+=1
print(count_young)
d
打印手机尾号是8的学生的名字
for item in list_students:
if item['phonenum'][-1] == '8':
print(item['name'])
e
打印最高分和对应的学生的名字
result_max=list_students[0]['result']
name=list_students[0]['name']
for item in list_students:
if item['result']>result_max:
result_max=item['result']
name=item['name']
for item in list_students:
if item['result']==result_max:
print(item['result'],item['name'])
f
将列表按学生成绩从大到小排序
for i in range(len(list_students)-1):
for j in range(i+1,len(list_students)):
if list_students[j]['result']>list_students[i]['result']:
list_students[i],list_students[j]=list_students[j],list_students[i]
print(list_students)
3
用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list1=['a','b','c','d','e']
list2=['d','e','f','g','h']
list3=['e','a','h','x','y']
a
求选课学生总共有多少人
print(len(set(list1)|set(list2)|set(list3)))
b
求只选了第一个学科的人的数量和对应的名字
print(len(set(list1)-set(list2)-set(list3)),set(list1)-set(list2)-set(list3))
c
求只选了一门学科的学生的数量和对应的名字
set1=set(list1)|set(list2)|set(list3)
set2=(set(list1)&set(list2))|(set(list2)&set(list3))|(set(list1)&set(list3))
print(len(set1-set2),set1-set2)
d
求只选了两门学科的学生的数量和对应的名字
set1=set(list1)|set(list2)|set(list3)
set2=(set(list1)&set(list2))|(set(list2)&set(list3))|(set(list1)&set(list3))
set3=set(list1)&set(list2)&set(list3)
print(len(set2-set3),set2-set3)
e
求选了三门学生的学生的数量和对应的名字
print(len(set(list1)&set(list2)&set(list3)),set(list1)&set(list2)&set(list3))