1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student = {
'name': '杨过', 'age': 22, 'grades': 88, 'nums': 13452563542, 'gender': '男'
}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{'name': '杨过', 'age': 22, 'grades': 88, 'nums': 13921151218, 'gender': '男'},
{'name': '小龙女', 'age': 18, 'grades': 95, 'nums': 13621512543, 'gender': '女'},
{'name': '李逍遥', 'age': 22, 'grades': 99, 'nums': 13235644842, 'gender': '男'},
{'name': '刘星', 'age': 15, 'grades': 55, 'nums': 13455541545, 'gender': '男'},
{'name': '金星', 'age': 50, 'grades': 76, 'nums': 13459846548, 'gender': '不明'},
{'name': '酒剑仙', 'age': 46, 'grades': 89, 'nums': 13452563547, 'gender': '男'}
]
# a.统计不及格学生的个数
count = 0
for student in students:
student_grades = student['grades']
if student_grades < 60:
count += 1
print('不及格学生人数为:%s' % count)
# b.打印不及格学生的名字和对应的成绩
for student in students:
student_grades = student['grades']
student_name = student['name']
if student_grades < 60:
print('不及格学生姓名及分数为:', student_name, student_grades)
# c.统计未成年学生的个数
count = 0
for student in students:
student_age = student['age']
if student_age < 18:
count += 1
print('未成年学生个数为:%s' % count)
# d.打印手机尾号是8的学生的名字
print('手机尾号为8的同学有:', end='')
for student in students:
student_nums = student['nums']
student_name = student['name']
if student_nums % 10 == 8:
print(student_name, end=' ')
print('')
# e.打印最高分和对应的学生的名字
highest_grades = 0
print('最高分及学生名字为:', end='')
for student in students:
student_grades = student['grades']
student_name = student['name']
if highest_grades < student_grades:
highest_grades = student_grades
print(highest_grades, end=' ')
for student in students:
if highest_grades == student['grades']:
print(student['name'], end='')
print('')
# f.将列表按学生成绩从大到小排序
key_value = {}
grades_list = []
new_students = []
for index in range(len(students)):
key_value[students[index]['grades']] = index
grades_list.append(students[index]['grades'])
print(key_value, grades_list)
grades_list.sort(reverse=True)
print(grades_list)
for grades in grades_list:
new_students.append(students[key_value[grades]])
print(new_students)
# 选排
length = len(students)
for x in range(length - 1):
for y in range(x+1, length):
if students[x]['grades'] < students[y]['grades']:
students[x], students[y] = students[y], students[x]
print(students)
# g.删除性别不明的所有学生
index = 0
while True:
if students[index]['gender'] == '不明':
del students[index]
index -= 1
index += 1
if index == len(students):
break
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
subject1 = ['a', 'b', 'c', 'e', 'f', 'g']
subject2 = ['a', 'b', 'c', 'd']
subject3 = ['a', 'd', 'f', 'g']
set_subject1 = set(subject1)
set_subject2 = set(subject2)
set_subject3 = set(subject3)
# a. 求选课学生总共有多少人
students_num = set_subject1 | set_subject2 | set_subject3
print(len(students_num))
# b. 求只选了第一个学科的人的数量和对应的名字
subject1_name = set_subject1 - set_subject2 - set_subject3
print(len(subject1_name), subject1_name)
# c. 求只选了一门学科的学生的数量和对应的名字
one_subject = set_subject1 ^ set_subject2 ^ set_subject3 - (set_subject1 & set_subject2)
print(len(one_subject), one_subject)
# d. 求只选了两门学科的学生的数量和对应的名字
two_subject = students_num - one_subject - (set_subject1 & set_subject2 & set_subject3)
print(len(two_subject), two_subject)
# e. 求选了三门学生的学生的数量和对应的名字
three_subject = set_subject1 & set_subject2 & set_subject3
print(len(three_subject), three_subject)