1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
g.删除性别不明的所有学生
all_student = [
{'name': '小米', 'age': 18, 'scores': 69, 'tel': '14734589870', 'sex': '男'},
{'name': '王明', 'age': 17, 'scores': 79, 'tel': '12345656848', 'sex': '女'},
{'name': '小华', 'age': 18, 'scores': 59, 'tel': '13345346566', 'sex': '男'},
{'name': '李华', 'age': 16, 'scores': 89, 'tel': '15326463438', 'sex': ''},
{'name': '张丽', 'age': 16, 'scores': 49, 'tel': '15845244564', 'sex': ''}
]
# 统计不及格学生的个数
# 打印不及格学生的名字和对应的成绩
# 统计未成年学生的个数
count = 0
age_count = 0
scores = []
max_scores = 0
for student in all_student:
if student['scores'] < 60:
count += 1
print('%s不及格,分数为%d' % (student['name'], student['scores']))
if student['age'] < 18:
age_count += 1
if max_scores < student['scores']:
max_scores = student['scores']
print('不及格的学生个数为%d' % count)
print('未成年的学生个数为%d' % age_count)
# 小华不及格,分数为59
# 张丽不及格,分数为49
# 不及格的学生个数为2
# 未成年的学生个数为3
# 打印手机尾号是8的学生的名字
print('手机尾号是8的学生有:')
for student in all_student:
if student['tel'][-1] == '8':
print(student['name'])
# 手机尾号是8的学生有:
# 王明
# 李华
# 打印最高分和对应的学生的名字
for student in all_student:
if student['scores'] == max_scores:
print('最高分为:%d' % max_scores, student['name'])
# 高分为:89 李华
# 将列表按学生成绩从大到小排序
length = len(all_student)
for lines in range(length):
for line in range(lines+1, length):
if all_student[lines]['scores'] < all_student[line]['scores']:
all_student[lines], all_student[line] = all_student[line], all_student[lines]
print(all_student)
# [{'name': '李华', 'age': 16, 'scores': 89, 'tel': '15326463438', 'sex': ''},
# {'name': '王明', 'age': 17, 'scores': 79, 'tel': '12345656848', 'sex': '女'},
# {'name': '小米', 'age': 18, 'scores': 69, 'tel': '14734589870', 'sex': '男'},
# {'name': '小华', 'age': 18, 'scores': 59, 'tel': '13345346566', 'sex': '男'},
# {'name': '张丽', 'age': 16, 'scores': 49, 'tel': '15845244564', 'sex': ''}]
length = len(all_student)
for lines in range(length-1, -1, -1):
if all_student[lines]['sex'] != '男' and all_student[lines]['sex'] != '女':
all_student.remove(all_student[lines])
print(all_student)
# [{'name': '王明', 'age': 17, 'scores': 79, 'tel': '12345656848', 'sex': '女'},
# {'name': '小米', 'age': 18, 'scores': 69, 'tel': '14734589870', 'sex': '男'},
# {'name': '小华', 'age': 18, 'scores': 59, 'tel': '13345346566', 'sex': '男'}]
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
computer_science = ['方平', '莫凡', '段婷', '王宇', '张萍', '廖凡']
university_science = ['方平', '段婷', '张涛', '鸿宇', '杨开', '苍猫']
european_music = ['鸿宇', '杨开', '段婷', '方平', '张萍', '莫凡']
# 求选课学生总共有多少人
all_lists = set(computer_science) | set(university_science) | set(european_music)
print(len(all_lists)) # 12
# 求只选了第一个学科的人的数量和对应的名字
lists = set(computer_science) - (set(university_science) | set(european_music))
print(len(lists), lists) # 2 {'廖凡', '王宇'}
# 求选了三门学生的学生的数量和对应的名字
three_object = set(computer_science) & set(university_science) & set(european_music)
print(len(three_object), three_object) # 2 {'段婷', '方平'}
# 只选了一门学科的学生的数量和对应的名字
one_object = set(computer_science) ^ set(university_science) ^ set(european_music) - three_object
print(len(one_object), one_object) # 4 {'张涛', '王宇', '苍猫', '廖凡'}
# 求只选了两门学科的学生的数量和对应的名字
tow_object = all_lists - three_object - one_object
print(len(tow_object), tow_object) # 4 {'鸿宇', '杨开', '张萍', '莫凡'}