1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student1 = {'name': '张三', 'age': 15, 'grades': {'chinese': 80, 'math': 90, 'English': 94}, 'tel': 13580345712, 'gender': '男'}
student2 = {'name': '李四', 'age': 14, 'grades': {'chinese': 87, 'math': 91, 'English': 88}, 'tel': 18700435219, 'gender': '女'}
student3 = {'name': '王五', 'age': 16, 'grades': {'chinese': 97, 'math': 76, 'English': 84}, 'tel': 18204799144, 'gender': '不明'}
}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{'name': '张三', 'age': 19, 'grades': {'chinese': 80, 'math': 70, 'English': 94}, 'tel': '13580345712', 'gender': '男'},
{'name': '李四', 'age': 17, 'grades': {'chinese': 49, 'math': 91, 'English': 88}, 'tel': '18700435218', 'gender': '女'},
{'name': '王五', 'age': 18, 'grades': {'chinese': 97, 'math': 76, 'English': 51}, 'tel': '18204799144', 'gender': '男'},
{'name': '赵六', 'age': 20, 'grades': {'chinese': 75, 'math': 54, 'English': 92}, 'tel': '13791443268', 'gender': '女'},
{'name': '钱七', 'age': 16, 'grades': {'chinese': 69, 'math': 86, 'English': 85}, 'tel': '15872396055', 'gender': '不明'},
{'name': '孙八', 'age': 15, 'grades': {'chinese': 83, 'math': 57, 'English': 49}, 'tel': '13391720023', 'gender': '男'}
]
a.统计不及格学生的个数
count_a = 0
for student in students:
for grade in student['grades']:
if student['grades'][grade] < 60:
count_a += 1
break
print('a.不及格学生的个数为:', count_a)
b.打印不及格学生的名字和对应的成绩
print('b.不及格学生的名字和对应的成绩为:')
for student in students:
for grade in student['grades']:
if student['grades'][grade] < 60:
print(student['name'], ':', grade, student['grades'][grade])
c.统计未成年学生的个数
count_c = 0
for student in students:
if student['age'] < 18:
count_c += 1
print('c.未成年学生的个数为:', count_c)
d.打印手机尾号是8的学生的名字
print('手机尾号是8的学生有:')
for student in students:
if student['tel'][-1] == '8':
print(student['name'], end=' ')
e.打印最高分和对应的学生的名字
top_chinese = top_math = top_english = 0
name_chinese = name_math = name_english = ''
for student in students:
if student['grades']['chinese'] > top_chinese:
top_chinese = student['grades']['chinese']
name_chinese = student['name']
if student['grades']['math'] > top_math:
top_math = student['grades']['math']
name_math = student['name']
if student['grades']['english'] > top_english:
top_english = student['grades']['english']
name_english = student['name']
print('最高分和对应的学生的名字是:')
print('chinese:', top_chinese, name_chinese)
print('math:', top_math, name_math)
print('english:', top_english, name_english)
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
n_students = []
while True:
max_set = set()
max_score = 0
for student in students:
if student['grades']['chinese'] > max_score:
max_score = student['grades']['chinese']
max_set = student
n_students.append(max_set)
students.remove(max_set)
if not students:
break
students = n_students
for i in students:
print(i)
g.删除性别不明的所有学生
for student in students[:]:
if student['gender'] == '不明':
students.remove(student)
for i in students:
print(i)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
chinese = ['张三', '小明', '小张', '小杨', '李四', '孙八', '王五']
english = ['小芳', '奇彦', '少从霜', '小杨', '李四', '孙八', '宋夫']
math = ['保才', '小明', '云秋白', '汪青易', '次浩', '孙八', '夕丹秋']
all_people = len(set(chinese) | set(english) | set(math))
print('选课学生总共有%d人。' % all_people)
chinese_people = set(chinese) - set(english) - set(math)
print('只选了第一门语文的人的数量为:%d' % len(chinese_people))
print('分别是:', chinese_people)
one_course = (set(chinese) ^ set(english) ^ set(math)) - (set(chinese) & set(english) & set(math))
print('只选了一门课的人的数量为:%d' % len(one_course))
print('分别是:', one_course)
two_course = (set(chinese) & set(english)) | (set(chinese) & set(math)) | (set(english) & set(math)) - (set(chinese) & set(english) & set(math))
print('只选了二门课的人的数量为:%d' % len(two_course))
print('分别是:', two_course)
three_course = set(chinese) & set(english) & set(math)
print('选了三门课的人的数量为%d:' % len(three_course), three_course)