##1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
dict1 = {'name': '瑞兹', 'age': 19, 'score': 90, 'phone':
19988880000, 'gender': 'man'}
list1 = [{'name': '瑞兹', 'age': 25, 'score': 95, 'phone': 19988880000, 'gender': 'man'},
{'name': '维恩', 'age': 16, 'score': 95, 'phone': 19988888888, 'gender': 'female'},
{'name': '寒冰', 'age': 15, 'score': 70, 'phone': 19988881111, 'gender': 'female'},
{'name': '龙女', 'age': 19, 'score': 80, 'phone': 19988882222, 'gender': 'female'},
{'name': '德莱文', 'age': 18, 'score': 50, 'phone': 19988883333, 'gender': 'unknown'},
{'name': '嘉文', 'age': 17, 'score': 59, 'phone': 19988885555, 'gender': 'man'}
]
1.统计不及格学生的个数
count = 0
for stu in list1:
if stu['score'] < 60:
count += 1
print(count)
2.打印不及格学生的名字和对应的成绩
for stu in list1:
if stu['score'] < 60:
print(stu['name'], stu['score'])
3.统计未成年学生的个数
for stu in list1:
if stu['age'] < 18:
count += 1
print('未成年学生个数为:', count)
4.打印手机尾号是8的学生的名字
for stu in list1:
if stu['phone'] % 10 == 8:
print('手机尾号是8的学生的名字是:', stu['name'])
5.打印最高分和对应的学生的名字
max_score = 0
for stu in list1:
if stu['score'] > max_score:
max_score = stu['score']
for stu in list1:
if max_score == stu['score']:
print('最高分的学生为:', stu['name'])
7.删除性别不明的所有学生
for stu in list1:
if stu['gender'] != 'man' and stu['gender'] != 'female':
list1.remove(stu)
print(list1)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = ['寒冰', '嘉文', '瑞兹', '德莱文']
maths = ['希维尔', '瑞兹', '维恩', '寒冰']
english = ['希维尔', '德莱文', '瑞兹']
1.求选课学生总共有多少人
set1 = set(chinese)
set2 = set(maths)
set3 = set(english)
print('选课学生总共有%d人' % len(set1 | set2 | set3), '分别为:', set1 | set2 | set3)
2.求只选了第一个学科的人的数量和对应的名字
print(len(set1 - set2 - set3), set1 - set2 - set3)
3.求只选了一门学科的学生的数量和对应的名字
count = 0
x = ''
y = ''
z = ''
for stu in set1:
if stu not in set2 and set3:
x = x + ' ' + stu
count += 1
for stu in set2:
if stu not in set1 and set3:
print(stu)
y = y + ' ' + stu
count += 1
for stu in set3:
if stu not in set1 and set2:
z = z + ' ' + stu
count += 1
print('只选了一门学科的学生的数量为:', count, '名字是:', x, y, z)
4.求只选了两门学科的学生的数量和对应的名字
x = ''
y = ''
z = ''
count = 0
for stu in set1:
if stu in set2 and stu not in set3 or stu in set3 and stu not in set2:
x = x + ' ' + stu
count += 1
for stu in set2:
if stu in set1 and stu not in set3 or stu in set3 and stu not in set1:
y = y + ' ' + stu
count += 1
for stu in set3:
if stu in set1 and stu not in set2 or stu in set2 and stu not in set1:
z = z + ' ' + stu
count += 1
print(x, y, z, count)
5.求选了三门学生的学生的数量和对应的名字
x = list(set(chinese) & set(maths) & set(english))
print(x)
print('===========================================================')
count = 0
for stu in chinese:
if stu in maths and stu in english:
print(stu)
for stu in maths:
if stu in chinese and stu in english:
print(stu)
for stu in english:
if stu in chinese and stu in maths:
print(stu)
count += 1
print(count)