-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [ { 'name': '小花', 'age': 17, 'score': 87, 'tel': '182*****2345', 'gender': '女' }, { 'name': '吴明', 'age': 22, 'score': 98, 'tel': '134*****6788', 'gender': '男' }, { 'name': '小强', 'age': 23, 'score': 56, 'tel': '145*****2367', 'gender': '不明' }, { 'name': '碧儿', 'age': 21, 'score': 99, 'tel': '139*****6758', 'gender': '女' }, { 'name': '张小小', 'age': 19, 'score': 78, 'tel': '123*****4589', 'gender': '不明' }, { 'name': '赵小苏', 'age': 25, 'score': 45, 'tel': '145*****3467', 'gender': '男' } ]
- 统计不及格学生的个数
count = 0 for x in students: if x.get('score') < 60: count += 1 print('不及格学生的个数:', count)
- 打印不及格学生的名字和对应的成绩
print('不及格的学生名字和对应的成绩') for x in students: if x.get('score') < 60: print(x['name'], x['score'])
- 统计未成年学生的个数
count = 0 for x in students: if x['age'] < 18: count += 1 print('未成年学生的个数:', count)
- 打印手机尾号是8的学生的名字
for x in students: if x['tel'][-1] == '8': print('手机尾号是8的学生为:', x['name'])
- 打印最高分和对应的学生的名字
# 方法1
# 第一次循环获取最高分
score_max = students[0]['score']
score_max_name = students[0]['name']
for x in students[1:]:
if score_max < x['score']:
score_max = x['score']
score_max_name = x['name']
print('最高分:', score_max, '学生姓名为:', score_max_name)
#第二次循环找到分数和最高峰相等的所有学生姓名
for stu in students:
if stu['score'] == score_max:
print('学生姓名为:', stu['name'])
print('--------------------')
#方法2
score_max = students[0]['score'] # 假设第一个学生的分数最高
names = [students[0]['name']] # 假设第一个学生的分数最高,保存第一个学生的姓名
for stu in students[1:]:
score = stu['score']
if score == score_max:
names.append(stu['name'])
elif score > score_max:
names.clear()
names.append(stu['name'])
score_max = score
print('最高分:', score_max, '学生姓名为:', names)
- 删除性别不明的所有学生
# 方法1
for stu in students[:]:
if stu['gender'] == '不明':
students.remove(stu)
print(students)
#方法2
list1 = [stu for stu in students if stu['gender'] != '不明']
print(list1)
- 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 方法1
for x in range(len(students)):
for y in range(len(students)-x-1):
if students[y]['score'] < students[y+1]['score']:
mmm = students[y+1]
students[y+1] = students[y]
students[y] = mmm
print(students)
# 方法2
students.sort(key=lambda item: item['score'], reverse=True)
print(students)
-
用三个元组表示三门学科的选课学生姓名(一个学生可以同时选多门课)
science = ('小花', '小明') art = ('小王', '张三', '李四') sing = ('小王', '张三', '李四', '小碧', '小芳')
- 求选课学生总共有多少人
list1 = list(science) + list(art) + list(sing) list2 = [] for x in list1: if x not in list2: list2.append(x) print('选课学生总共有:', len(list2))
- 求只选了第一个学科的人的数量和对应的名字
count = 0 for x in science: if x not in art and x not in sing: count += 1 print(x, end=' ') print() print('只选了第一个学科的人的数量:', count)
- 求只选了一门学科的学生的数量和对应的名字
count = 0 list1 = list(science) + list(art) + list(sing) for x in list1: if list1.count(x) == 1: count += 1 print(x, end=' ') print() print('只选了一门学科的学生的数量:', count)
- 求只选了两门学科的学生的数量和对应的名字
list1 = list(science) + list(art) + list(sing) list2 = [] for x in list1: if list1.count(x) == 2 and x not in list2: list2.append(x) for y in list2: print(y, end=' ') print() print('只选了两门学科的学生的数量:', len(list2))
5.求选了三门学生的学生的数量和对应的名字
list1 = list(science) + list(art) + list(sing) list3 = [] for x in list1: if list1.count(x) == 3 and x not in list3: list3.append(x) for y in list3: print(y, end=' ') print() print('选了三门学生的学生的数量:', len(list3))