-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [{'name': '张三', 'age': 23, 'score': 62, 'cell': '110', 'gender': '不明'}, {'name': '李四', 'age': 24, 'score': 70, 'cell': '120', 'gender': '不明'}, {'name': '王二', 'age': 22, 'score': 98, 'cell': '119', 'gender': '男'}, {'name': '二狗', 'age': 2, 'score': 42, 'cell': '114', 'gender': '不明'}, {'name': '小翠', 'age': 18, 'score': 95, 'cell': '118', 'gender': '不明'}, {'name': '小花', 'age': 12, 'score': 8, 'cell': '1168', 'gender': '女'}]
-
统计不及格学生的个数
count1 = 0 for x in students: if x['score'] < 60: count1 += 1 print('不及格学生个数:', count1)
-
打印不及格学生的名字和对应的成绩
for x in students: if x['score'] < 60: print('不及格学生姓名:', x['name'], ';分数:', x['score'], sep='')
-
统计未成年学生的个数
count1 = 0 for x in students: if x['age'] < 18: count1 += 1 print('未成年学生的个数:', count1)
-
打印手机尾号是8的学生的名字
print('手机尾号是8的学生有:', end='') # 方法一: for x in students: if int(x['cell']) % 10 == 8: print(x['name'], sep='', end=' ') print() # 方法二: for stu in students: if stu['cell'][-1] == '8': print(stu['name'])
-
打印最高分和对应的学生的名字
# 方法一: max_score = students[0]['score'] for stu in students[1:]: score = stu['score'] if score > max_score: max_score = score for stu in students: if stu['score'] == max_score: print(stu['name'], max_score) # 方法二: max_score = students[0]['score'] names = [students[0]['name']] for stu in students[1:]: score = stu['score'] if score == max_score: names.append(stu['name']) elif score > max_score: max_score = score names.clear() names.append(stu['name']) print(names, max_score)
-
删除性别不明的所有学生
# 方法一: students_copy = students.copy() for stu in students: if stu['gender'] == '不明': students_copy.remove(stu) print(students_copy) # 方法二: new_students = [stu for stu in students if stu['gender'] != '不明'] print(new_students)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students.sort(key=lambda item: item['score'], reverse=True) print(students)
-
-
用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课)
Math = ('张三', '李四') English = ('张三', '王二') Music = ('张三', '李四', '王二') subjects = [Math, English, Music]
-
求选课学生总共有多少人
list1 = [] for x in subjects: for y in x[0:]: if y not in list1: list1.append(y) print('选课学生共有:', len(list1))
-
求只选了第一个学科的人的数量和对应的名字
print('只选了第一个学科的人数:', len(subjects[0])) for x in subjects[0][:]: print(x)
-
求只选了一门学科的学生的数量和对应的名字
list1 = [] count1 = 0 for x in subjects[0]: if x not in subjects[1] and x not in subjects[2]: list1.append(x) count1 += 1 for y in subjects[1]: if y not in subjects[0] and y not in subjects[2]: list1.append(y) count1 += 1 for z in subjects[2]: if z not in subjects[0] and z not in subjects[1]: list1.append(z) count1 += 1 print('只选了一门学科的学生人数:', count1, ';名字为:', list1)
-
求只选了两门学科的学生的数量和对应的名字
list1 = [] count1 = 0 for x in subjects[0]: if x in subjects[1] and x not in subjects[2]: list1.append(x) count1 += 1 for y in subjects[1]: if y not in subjects[0] and y in subjects[2]: list1.append(y) count1 += 1 for z in subjects[2]: if z in subjects[0] and z not in subjects[1]: list1.append(z) count1 += 1 print('只选了两门学科的学生人数:', count1, ';名字为:', list1)
-
求选了三门学生的学生的数量和对应的名字
list1 = [] count1 = 0 for x in subjects[0]: if x in subjects[1] and x in subjects[2]: list1.append(x) count1 += 1 print('选了三门学科的学生人数:', count1, ';名字为:', list1)
-
day7-字典作业
最新推荐文章于 2024-04-12 09:24:16 发布