-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
name = { 'stu':[ {'name':'stu1','age':18,'score':80,'tel':'123','gender':'男'}, {'name':'stu2','age':20,'score':74,'tel':'134','gender':'女'}, {'name':'stu3','age':19,'score':45,'tel':'148','gender':'男'}, {'name':'stu4','age':22,'score':90,'tel':'156','gender':'不明'}, {'name':'stu5','age':24,'score':56,'tel':'173','gender':'女'}, {'name':'stu6','age':21,'score':60,'tel':'192','gender':'男'} ] }
-
统计不及格学生的个数
count = 0 all_stu = name['stu'] for stu in all_stu: if stu['score'] < 60: count += 1 print('不及格人数:',count)
-
打印不及格学生的名字和对应的成绩
all_stu = name['stu'] for stu in all_stu: if stu['score'] < 60: print(stu['name'])
-
打印手机尾号是8的学生的名字
all_stu = name['stu'] for stu in all_stu: if int(stu['tel']) % 10 == 8: print(stu['name'])
-
打印最高分和对应的学生的名字
Max_Score = name[0]['score'] for x in name[1:]: score = x['score'] if score > Max_Score: Max_Score = score for x in name: if x['score'] == Max_Score: print(Max_Score, ':', x['name'])
-
删除性别不明的所有学生
for stu in name: if stu['gender'] == '不明': name.remove(stu) print(name)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
-
-
用三个集合表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
math = ['C', 'E', 'F', 'J', 'K', 'L', 'I']
english = ['A', 'B', 'C', 'E', 'M', 'N']
-
求选课学生总共有多少人
print(len(list(dict.fromkeys(chinese + math + english))))
-
求只选了第一个学科的人的数量和对应的名字
combine = chinese + math + english count = 0 print('只选了第一门学科的有:', end='') for x in chinese: if x not in math and x not in english: print(x, end=' ') count += 1 print(' 总共有%d人' % count)
-
求只选了一门学科的学生的数量和对应的名字
-
求只选了两门学科的学生的数量和对应的名字
-
求选了三门学生的学生的数量和对应的名字
all_stu = [] for x in list(dict.fromkeys(chinese + math + english)): if x in chinese and x in math and x in english: all_stu.append(x) print('三门都选了的人数:', len(all_stu), ' 对应的学生是:', all_stu, )
-