1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
dict_students = {'name': '小明', 'age': 15, 'grade': 66, 'tel': 12345679810, 'gender': '男'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
list_students = [{'name': '小明', 'age': 15, 'grade': 66, 'tel': 12345679810, 'gender': '男'},
{'name': '小东', 'age': 16, 'grade': 23, 'tel': 12345623810, 'gender': '男'},
{'name': '赵红', 'age': 17, 'grade': 56, 'tel': 12345743320, 'gender': '男'},
{'name': '李四', 'age': 13, 'grade': 88, 'tel': 12352349810, 'gender': '男'},
{'name': '张三', 'age': 19, 'grade': 70, 'tel': 12345452110, 'gender': '男'},
{'name': '王五', 'age': 14, 'grade': 60, 'tel': 12432456810, 'gender': '男'}]
a.统计不及格学生的个数
fail_num = 0
for stu in list_students:
if stu['grade'] < 60 :
fail_num += 1
else:
print(fail_num)
b.打印不及格学生的名字和对应的成绩
fail_stus = []
for stu in list_students:
if stu['grade'] < 60 :
fail_stus.append(stu)
for fail_stu in fail_stus:
print(fail_stu['name'], fail_stu['grade'])
c.统计未成年学生的个数
nonage_num = 0
for stu in list_students:
if stu['age'] < 18:
nonage_num += 1
d.打印手机尾号是8的学生的名字
stu_names = []
for stu in list_students:
if stu['tel'] % 10 == 8:
stu_names.append(stu['name'])
else:
print(stu_names)
e.打印最高分和对应的学生的名字
stu_names = [list_students[0]['name']]
max_grade = list_students[0]['grade']
for stu in list_students:
if stu['grade'] > max_grade:
max_grade = stu['grade']
stu_names.clear()
stu_names.append(stu['name'])
elif stu['grade'] == max_grade:
stu_names.append(stu['name'])
else:
print(stu_names)
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
list_stu = []
list_grade = []
for stu in list_students:
list_grade.append(stu['grade'])
else:
print(list_grade)
list_grade.sort(reverse=True)
for grade in list_grade:
for stu in list_students[:]:
if stu['grade'] == grade:
list_stu.append(stu)
list_students.remove(stu)
else:
for index in list_stu:
print(index)
g.删除性别不明的所有学生
for stu in list_students[:]:
if stu['gender'] == '不明':
list_students.remove(stu)
else:
print(list_students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
subject_math = ['小明', '小方', '小呆', '大东', '赵四']
subject_english = ['李四', '王五', '小呆', '大东', '赵四']
subject_language = ['李四', '张三', '王五', '大东', '赵四']
set_1 = set(subject_math)
set_2 = set(subject_english)
set_3 = set(subject_language)
a. 求选课学生总共有多少人
print(len(set_1 | set_2 | set_3))
b. 求只选了第一个学科的人的数量和对应的名字
new_set = set_1-set_2-set_3
print(len(new_set))
print(new_set)
c. 求只选了一门学科的学生的数量和对应的名字
new_set_1 = set_1-set_2-set_3
new_set_2 = set_2-set_1-set_3
new_set_3 = set_3-set_1-set_2
print(len(new_set_1 | new_set_2 | new_set_3))
print(new_set_1 | new_set_2 | new_set_3)
d. 求只选了两门学科的学生的数量和对应的名字
new_set_1 = (set_1 & set_2)-set_3
new_set_2 = (set_2 & set_3)-set_1
new_set_3 = (set_3 & set_1)-set_2
new_set = new_set_1 | new_set_2 | new_set_3
print(len(new_set))
print(new_set)
e. 求选了三门学生的学生的数量和对应的名字
new_set = set_1 & set_2 & set_3
print(len(new_set))
print(new_set)