1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student = {
'name': None, 'age': None, 'score': None, 'tel': None, 'gender': None
}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': 18845628134, 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': 15764529145, 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': 13589648164, 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': 18856421345, 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 40, 'tel': 13648975641, 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': 18542368984, 'gender': '女'}
]
a.统计不及格学生的个数
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': 18845628134, 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': 15764529145, 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': 13589648164, 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': 18856421345, 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 40, 'tel': 13648975641, 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': 18542368984, 'gender': '女'}
]
count = 0
for student in students:
score = student['score']
if score < 60:
count += 1
print('不及格的同学有', count, '名') # 不及格的同学有 2 名
b.打印不及格学生的名字和对应的成绩
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': 18845628134, 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': 15764529145, 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': 13589648164, 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': 18856421345, 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 40, 'tel': 13648975641, 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': 18542368984, 'gender': '女'}
]
count = 0
for student in students:
score = student['score']
if score < 60:
count += 1
print(student['name'], '同学不及格,成绩为', score)
print('不及格的同学有', count, '名')
# 张三 同学不及格,成绩为 55
# 吴明 同学不及格,成绩为 40
# 不及格的同学有 2 名
c.统计未成年学生的个数
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648164', 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 40, 'tel': '13648975641', 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368984', 'gender': '女'}
]
count = 0
for student in students:
age = student['age']
if age < 18:
count += 1
print('有', count, '名同学还未成年') # 有 2 名同学还未成年
d.打印手机尾号是8的学生的名字
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648168', 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 40, 'tel': '13648975648', 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
]
print('手机尾号是8的同学有:', end='')
for student in students:
tel_tail = student['tel'][-1]
if tel_tail == '8':
print(student['name'], end='、')
print('\b') # 手机尾号是8的同学有:张三、吴明、王东
e.打印最高分和对应的学生的名字
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648168', 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 88, 'tel': '13648975648', 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
]
max_score = 0
for student in students:
score = student['score']
if max_score < score:
max_score = score
print('最高分是', max_score, ',对应的同学是:', end='')
for student in students:
if max_score == student['score']:
print(student['name'], end='、')
print('\b') # 最高分是 88 ,对应的同学是:李四、吴明
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 插入排序法
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648168', 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 88, 'tel': '13648975648', 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
]
def sort_score(all_stu, ord_stu, len_stu):
if ord_stu == len_stu:
return
else:
if all_stu[ord_stu]['score'] > all_stu[ord_stu - 1]['score']:
if ord_stu == 1:
score1 = all_stu[ord_stu]
score2 = all_stu[ord_stu - 1]
all_stu[ord_stu], all_stu[ord_stu - 1] = score2, score1
else:
temp = all_stu[ord_stu]
for x in range(ord_stu - 1, -1, -1):
if temp['score'] <= all_stu[x]['score']:
del all_stu[ord_stu]
all_stu.insert(x + 1, temp)
break
return sort_score(all_stu, ord_stu + 1, len_stu)
len_students = len(students)
ord1 = 1
sort_score(students, ord1, len_students)
for student in students:
print(student)
# 方法二
# 先取出所有的分数
scores = []
for student in students:
scores.append(student['score'])
scores.sort(reverse=True)
# 一个一个取出排序的分数,然后再看那个学生的分数和取出来的分数一样
new_students = []
for score in scores:
for student in students[:]:
if score == student['score']:
new_students.append(student)
students.remove(student)
break
for new_student in new_students:
print(new_student)
# 方法3:选择排序
len_students = len(students)
for x in range(len_students - 1):
for y in range(x+1, len_students):
if students[x]['score'] < students[y]['score']:
students[x], students[y] = students[y], students[x]
for student in students:
print(student)
"""
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'}
{'name': '吴明', 'age': 16, 'score': 88, 'tel': '13648975648', 'gender': '男'}
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'}
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'}
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648168', 'gender': '不明'}
"""
g.删除性别不明的所有学生
students = [
{'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'},
{'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'},
{'name': '张三', 'age': 20, 'score': 55, 'tel': '13589648168', 'gender': '不明'},
{'name': '王五', 'age': 23, 'score': 72, 'tel': '18856421345', 'gender': '不明'},
{'name': '吴明', 'age': 16, 'score': 88, 'tel': '13648975648', 'gender': '男'},
{'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
]
# 方法1
students2 = students.copy()
for student in students2:
if student['gender'] == '不明':
students.remove(student)
for student in students:
print(student)
# 方法2
stu_len = len(students)
for i in range(stu_len-1, -1, -1):
if students[i]['gender'] == '不明':
del students[i]
for student in students:
print(student)
# 方法3
students2 = []
for student in students:
if not student['gender'] == '不明':
students2.append(student)
for student in students2:
print(student)
# 方法4
index = 0
while index < len(students):
if students[index]['gender'] == '不明':
del students[index]
continue
index += 1
for student in students:
print(student)
# {'name': '小明', 'age': 18, 'score': 76, 'tel': '18845628134', 'gender': '男'}
# {'name': '李四', 'age': 12, 'score': 88, 'tel': '15764529145', 'gender': '女'}
# {'name': '吴明', 'age': 16, 'score': 88, 'tel': '13648975648', 'gender': '男'}
# {'name': '王东', 'age': 19, 'score': 60, 'tel': '18542368988', 'gender': '女'}
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
math = ['tom', 'tim', 'dora', 'anna', 'john', 'hen']
english = ['tina', 'amy', 'anna', 'dom', 'john']
chinese = ['susan', 'li', 'john', 'dom', 'tim', 'anna']
all_persons = len(set(math) | set(english) | set(chinese))
print('选课学生有', all_persons, '人')
# 选课学生有 11 人
b. 求只选了第一个学科的人的数量和对应的名字
math = ['tom', 'tim', 'dora', 'anna', 'john', 'hen']
english = ['tina', 'amy', 'anna', 'dom', 'john']
chinese = ['susan', 'li', 'john', 'dom', 'tim', 'anna']
only_math = set(math) - set(english) - set(chinese)
all_persons = len(only_math)
print('选择数学的有', all_persons, '人,他们的名字是:', end='')
for name in only_math:
print(name, end='、')
print('\b')
# 只选择数学的有 3 人,他们的名字是:hen、dora、tom
c. 求只选了一门学科的学生的数量和对应的名字
math = ['tom', 'tim', 'dora', 'anna', 'john', 'hen']
english = ['tina', 'amy', 'anna', 'dom']
chinese = ['susan', 'li', 'john', 'dom', 'tim']
all_persons = (set(math) - set(english) - set(chinese)) | \
(set(english) - set(math) - set(chinese)) | \
(set(chinese) - set(english) - set(math))
# all_persons = (set(math) | set(english) | set(chinese)) - \
((set(math) & set(english)) | (set(math) & set(chinese)) | (set(english) & set(chinese)))
# all_persons = (set(math) ^ set(english) ^ set(chinese)) - (set(math) & set(english) & set(chinese))
print('只选择了一门学科的有', len(all_persons), '人,他们的名字是:', end='')
for name in all_persons:
print(name, end='、')
print('\b')
# 只选择了一门学科的有 7 人,他们的名字是:tom、amy、dora、susan、li、tina、hen
d. 求只选了两门学科的学生的数量和对应的名字
math = ['tom', 'tim', 'dora', 'anna', 'john', 'hen']
english = ['tina', 'amy', 'anna', 'dom', 'john']
chinese = ['susan', 'li', 'john', 'dom', 'tim', 'anna']
all_persons = ((set(math) & set(english)) | (set(chinese) & set(english)) |(set(math) & set(chinese))) \
- (set(math) & set(english) & set(chinese))
print('只选择了两门学科的有', len(all_persons), '人,名字是:', end='')
for name in all_persons:
print(name, end='、')
print('\b')
# 只选择了两门学科的有 2 人,名字是:dom、tim
e. 求选了三门学生的学生的数量和对应的名字
math = ['tom', 'tim', 'dora', 'anna', 'john', 'hen']
english = ['tina', 'amy', 'anna', 'dom', 'john']
chinese = ['susan', 'li', 'john', 'dom', 'tim', 'anna']
all_persons = set(math) & set(english) & set(chinese)
print('选择了三门学科的有', len(all_persons), '人,名字是:', end='')
for name in all_persons:
print(name, end='、')
print('\b')
# 选择了三门学科的有 2 人,名字是:anna、john