day7-字典作业
作者:互联网
-
定义一个列表,在列表中保存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()
-
打印最高分和对应的学生的名字
scores = 0 name1 = '' for x in students: if x['score'] > scores: scores = x['score'] name1 = x['name'] print(scores, name1)
-
删除性别不明的所有学生
for x in students: if x['gender'] == '不明': x.clear() 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)
-
标签:score,list1,day7,作业,学生,print,subjects,count1,字典 来源: https://blog.csdn.net/HenryWang0412/article/details/120444268