1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
student_information = {'name': '小明', 'age': 18, 'scores': '88', 'tel': '5223128'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
all_student = [
{'name': '小明', 'age': 18, 'scores': 88, 'tel': '5223128'},
{'name': '小红', 'age': 19, 'scores': 78, 'tel': '5223126'},
{'name': '小丽', 'age': 20, 'scores': 40, 'tel': '5223125'},
{'name': '小段', 'age': 19, 'scores': 42, 'tel': '5223128'},
{'name': '小马', 'age': 16, 'scores': 97, 'tel': '5223122'},
{'name': '小东', 'age': 15, 'scores': 60, 'tel': '5223121'}
]
# a.统计不及格学生的个数
count = 0
for score in all_student:
if score['scores'] < 60:
count += 1
print('不及格学生的数量: %d' % count)
# b.打印不及格学生的名字和对应的成绩
for score in all_student:
if score['scores'] < 60:
print(score['name'], score['scores'])
# c.统计未成年学生的个数
count = 0
for score in all_student:
if score['age'] < 18:
count += 1
print('未成年学生的数量: %d' % count)
# d.打印手机尾号是8的学生的名字
for num in all_student:
new_num = list(num['tel'])
if new_num[-1:-2:-1] == ['8']:
print(num['name'], num['tel'])
# e.打印最高分和对应的学生的名字
max1 = 60
for score in all_student:
new_score = score['scores']
if new_score > max1:
max1 = new_score
for score in all_student:
if score['scores'] == max1:
print(score['name'], score['scores'])
# f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
all_student.sort(key=lambda keys:keys['scores'],reverse=True)
for i in all_student:
print(i)
# 方法二
length = len(all_student)
for i in range(length - 1):
for j in range(i+1, length):
if all_student[j]['scores'] > all_student[i]['scores']:
all_student[i], all_student[j] = all_student[j], all_student[i]
print(all_student)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list1 = ['小明', '小马', '小东', '小红', '小兰']
list2 = ['小东', '小智', '小红', '小新', '小马']
list3 = ['小马', '小小', '小新', '小兰', '小丽', '小红']
# a. 求选课学生总共有多少人
print('选课学生总共', len(set(list1) | set(list2) | set(list3)), '人')
# b. 求只选了第一个学科的人的数量和对应的名字
set1 = set(list1)
set2 = set(list2)
set3 = set(list3)
new_set1 = set1 - set2 - set3
print('姓名:', ''.join(list(new_set1)), len(new_set1), '人')
# c. 求只选了一门学科的学生的数量和对应的名字
new_set2 = set2 - set1 - set3
new_set3 = set3 - set1 - set2
print('姓名:%s共%d人' % (','.join(list(new_set1 | new_set2 | new_set3)), len(new_set1 | new_set2 | new_set3)))
# d. 求只选了两门学科的学生的数量和对应的名字
new_set4 = set1 & set2
new_set5 = set2 & set3
new_set6 = set3 & set1
new_set7 = new_set4 | new_set5 | new_set6
new_set8 = new_set7 - (set1 & set2 & set3)
print('姓名:%s共%d人' % (','.join(list(new_set8)), len(new_set8)))
# e. 求选了三门学生的学生的数量和对应的名字
new_set9 = set1 & set2 & set3
print('姓名:%s共%d人' % (','.join(list(new_set9)), len(new_set9)))
网友评论