1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
stu = {'name': 'xxx', 'age': 18, 'grade': 60, 'tel': 12345678, 'sex': '男/女/不明'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
g.删除性别不明的所有学生
stu_list = [
{'name': 'a', 'age': 18, 'grade': 60, 'tel': '12345678', 'sex': '男'},
{'name': 'b', 'age': 20, 'grade': 34, 'tel': '12353634', 'sex': '女'},
{'name': 'c', 'age': 19, 'grade': 90, 'tel': '13545658', 'sex': '男'},
{'name': 'd', 'age': 16, 'grade': 78, 'tel': '23543685', 'sex': '不明'},
{'name': 'e', 'age': 17, 'grade': 13, 'tel': '90584678', 'sex': '男'},
{'name': 'f', 'age': 19, 'grade': 62, 'tel': '34879778', 'sex': '男'}
]
ungrade_count = 0
teen_count = 0
tel_name = []
max_score = 0
new_list = []
for i in stu_list:
# 打印不及格学生的名字和对应的成绩
if i['grade'] < 60:
# 统计不及格学生的个数
ungrade_count += 1
print('名字:', i['name'],'成绩:', i['grade'])
# 统计未成年学生的个数
if i['age'] < 18:
teen_count += 1
# 打印手机尾号是8的学生的名字
# print(i['tel'], type(i['tel']))
if i['tel'][-1] == '8':
tel_name.append(i['name'])
# print(i['tel'])
# 打印最高分和对应的学生的名字
if i['grade'] >= max_score:
max_score = i['grade']
max_name = i['name']
print('不及格人数:', ungrade_count)
print('未成年人数:', teen_count)
print('号码尾号为8的学生姓名:', tel_name)
print('最高分为:', max_score, '名字为:', max_name)
print('========================================================')
# 将列表按学生成绩从大到小排序
# 高级函数法(了解)
stu_list.sort(key=lambda item:item['grade], reverse=True)
print(stu_list)
print('========================================================')
# 删除性别不明的所有学生
for i in stu_list:
if i['sex'] == '不明':
# print(stu_list.index(i))
del stu_list[stu_list.index(i)]
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
subject_list = {
# a,b,c...分别代表学生姓名,1,2,3代表课程
'a': ['1', '2', '3'],
'b': ['1', '3'],
'c': ['1', '2'],
'd': ['3'],
'e': ['2', '3'],
'f': ['2', '3']
}
# 求选课学生总共有多少人
print('选课学生人数为:', len(subject_list))
# 求只选了第一个学科的人的数量和对应的名字
count1 = 0
for i in ['a', 'b', 'c', 'd', 'e', 'f']:
if '1' in subject_list[i]:
count1 += 1
print(i)
print('选了1课程的人数为:', count1)
# 求只选了一门学科的学生的数量和对应的名字
one_count = 0
for j in ['a', 'b', 'c', 'd', 'e', 'f']:
# print(subject_list[j])
if len(subject_list[j]) == 1:
one_count += 1
print(j, '选一门课程的人数为:', one_count)
# 求只选了两门学科的学生的数量和对应的名字
two_count = 0
for x in ['a', 'b', 'c', 'd', 'e', 'f']:
if len(subject_list[x]) == 2:
two_count += 1
print(x)
print('选二门课程的人数为:', two_count)
# 求选了三门学生的学生的数量和对应的名字
three_count = 0
for y in ['a', 'b', 'c', 'd', 'e', 'f']:
if len(subject_list[y]) == 3:
three_count += 1
print(y)
print('选三门课程的人数为:', three_count)