1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
stu_info = {'name': 'Chris Lee', 'age': 18, 'score': 90, 'tel': ‘110120’}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
stu_info1 = {''name': '小龙女', 'age': 18, 'score': 90, 'tel': ‘110128'}
stu_info2 = {''name': '三太子', 'age': 15, 'score': 49, 'tel': ’120119'}
stu_info3 = {''name': '林黛玉', 'age': 14, 'score': 61, 'tel': ‘120138'}
stu_info4 = {''name': '锦毛鼠', 'age': 19, 'score': 78, 'tel': ‘119145'}
stu_info5 = {''name': '玉兔精', 'age': 17, 'score': 50, 'tel': ‘134156'}
stu_info6 = {''name': '金角大王', 'age': 20, 'score': 12, 'tel': ‘122789'}
list1 = [stu_info1, stu_info2, stu_info3, stu_info4, stu_info5, stu_info6]
a.统计不及格学生的个数
count = 0 # 统计不及格的学生的个数
for item in list1:
if item['score'] < 60:
count += 1
print(count)
b.打印不及格学生的名字和对应的成绩
for item in list1:
if item['score'] < 60:
print(item['name'], item['score'])
c.统计未成年学生的个数
count1 = 0 # 定义一个变量统计未成年学生的个数
for item in list1:
if item['age'] < 18:
count1 += 1
print(count1)
d.打印手机尾号是8的学生的名字
for item in list1:
if item['tel'][-1] == '8':
print(item['name'])
e.打印最高分和对应的学生的名字
max_score = 90
best_student = '小龙女'
for item in list1:
if item['score'] > max_score:
max_score = item['score']
best_student = item['name']
print(max_score, best_student)
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for i in range(len(list1)):
for j in range(len(list1)):
if list1[i].get('score') > list1[j].get('score'):
list1[i], list1[j] = list1[j], list1[i]
print(list1)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list_python = ['小狗', '小龙', '小虎', '小龙虾', '小兔子', '小猴子']
list_c = ['大黑', '大黄', '小虎', '小猴子' ]
list_java = ['大黄', '小虎, '小兔子', '大马哈', '海绵宝宝']
a. 求选课学生总共有多少人
total_names = list(set(list_python) | set(list_c) | set(list_java))
print(len(total_names))
b. 求只选了第一个学科的人的数量和对应的名字
python_names = list(set(list_python) - set(list_c) - set(list_java))
print(python_names, len(python_names))
c. 求只选了一门学科的学生的数量和对应的名字
subject3 = list(set(list_python) & set(list_c) & set(list_java))
print(subject3) # 选了三门课的学生
single_subject = list((set(list_python) ^ set(list_c) ^ set(list_java)) - set(subject3)) # 只选了一门课的学生
print(single_subject, len(single_subject))
d. 求只选了两门学科的学生的数量和对应的名字
subject = list((set(list_python) & set(list_java)) | (set(list_python) & set(list_c)) | (set(list_c) & set(list_java)))
subject2 = list(set(subject) - set(subject3)) #选了两门课的学生
print(subject2, len(subject2))
e. 求选了三门学生的学生的数量和对应的名字
subject3 = list(set(list_python) & set(list_c) & set(list_java))
print(subject3)