-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
-
统计不及格学生的个数
list1 = [ {'name': '晨晨', 'age':18, 'score': 78, 'tel': '123', 'gender': '男'}, {'name': '陈来', 'age':20, 'score': 80, 'tel': '321', 'gender': '不明'}, {'name': '陈昕', 'age':28, 'score': 98, 'tel': '653', 'gender': '女'}, {'name': '小新', 'age':32, 'score': 65, 'tel': '783', 'gender': '男'}, {'name': '小明', 'age':17, 'score': 24, 'tel': '988', 'gender': '女'}, {'name': '小红', 'age':14, 'score': 54, 'tel': '903', 'gender': '男'} ] count = 0 for x in list1: score = x['score'] if score < 60: count += 1 print(count) # 2
-
打印不及格学生的名字和对应的成绩
for x in list1: score = x['score'] name = x['name'] if score < 60: print(name, score) #小明 24 小红 54
-
统计未成年学生的个数
count = 0 for x in list1: age = x['age'] if age < 18: count += 1 print(count) # 2
-
打印手机尾号是8的学生的名字
for x in list1: name = x['name'] tel = int(x['tel']) if tel % 10 == 8: print(name, tel) # 小明 988
-
打印最高分和对应的学生的名字
max1 = 0 for x in list1: score = x['score'] if score > max1: max1 = score name = x['name'] print(max1,name) # 98 陈昕
-
删除性别不明的所有学生
for x in list1: if x['gender'] == '不明': list1.remove(x) print(list1) 结果: [{'name': '晨晨', 'age': 18, 'score': 78, 'tel': '123', 'gender': '男'}, {'name': '陈昕', 'age': 28, 'score': 98, 'tel': '653', 'gender': '女'}, {'name': '小新', 'age': 32, 'score': 65, 'tel': '783', 'gender': '男'}, {'name': '小明', 'age': 17, 'score': 24, 'tel': '988', 'gender': '女'}, {'name': '小红', 'age': 14, 'score': 54, 'tel': '903', 'gender': '男'}]
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
-
-
用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课)
-
求选课学生总共有多少人
stu1 = ('张三', '李思', '陈来') stu2 = ('张三', '小新', '小明') stu3 = ('张四', '小名', '小红', '小张') stu4 = () new_stu4 = [x for x in stu1 if x not in stu4] new_stu5 = new_stu4 + [y for y in stu2 if y not in new_stu4] new_stu6 = new_stu5 + [z for z in stu3 if z not in new_stu5] print('选修学生总人数:', len(new_stu6)) 结果:选修学生总人数: 9
-
求只选了第一个学科的人的数量和对应的名字
first = [] for stu in stu1: if stu not in stu2 and stu not in stu3: first.append(stu) print(first, len(first)) # ['李思', '陈来'] 2
-
求只选了一门学科的学生的数量和对应的名字
stu1 = ('张三', '李思', '陈来') stu2 = ('张三', '小新', '小明') stu3 = ('张四', '小名', '小红', '小张', '张三') math1 = [] english1 = [] history1 = [] for x in stu1: if x not in stu2 and x not in stu3: math1.append(x) print(math1, len(math1)) english1 = [y for y in stu2 if (y not in stu1 and y not in stu3)] print(english1, len(english1)) history1 = [z for z in stu3 if (z not in stu1 and z not in stu2)] print(history1, len(history1)) 结果: ['李思', '陈来'] 2 ['小新', '小明'] 2 ['张四', '小名', '小红', '小张'] 4
-
求只选了两门学科的学生的数量和对应的名字(有问题)
stu1 = ('张三', '李思', '陈来') stu2 = ('张三', '小新', '小明', '陈来') stu3 = ('张四', '小名', '小红', '小张', '张三') stu5 = ['张三', '李思', '陈来', '小新', '小明', '张四', '小名', '小红', '小张'] stu6 = [] count = 0 for x in stu5: if (x in stu1 and x in stu2 and x not in stu3) or (x in stu2 and x in stu3 and x not in stu1) or (x in stu1 and x in stu3 and x not in stu2): stu6 += [x] print(stu6) # ['陈来']
-
求选了三门学生的学生的数量和对应的名字
-
stu1 = ('张三', '李思', '陈来')
stu2 = ('张三', '小新', '小明', '陈来')
stu3 = ('张四', '小名', '小红', '小张', '张三')
for x in stu5:
if( x in stu1 and x in stu2 and x in stu3):
stu6 += [x]
print(stu6) # ['张三'] 3