1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话
stu_dict = {'name': '小花', 'age': 18, 'scores': 99, 'del': '118'}
print(stu_dict)
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students = [
{'name': '鲁班', 'age': 10, 'scores': 59, 'del': '111'},
{'name': '大乔', 'age': 18, 'scores': 90, 'del': '111'},
{'name': '小乔', 'age': 16, 'scores': 80, 'del': '112'},
{'name': '程咬金', 'age': 30, 'scores': 70, 'del': '113'},
{'name': '老夫子', 'age': 65, 'scores': 50, 'del': '114'},
{'name': '貂蝉', 'age': 20, 'scores': 99, 'del': '118'}]
a.统计不及格学生的个数
count = 0
for item in students:
if item['scores'] < 60:
count += 1
print("不及格的人数为:", count)
b.打印不及格学生的名字和对应的成绩
for item in students:
if item['scores'] < 60:
print(item['name'], item['scores'])
c.统计未成年学生的个数
count = 0
for item in students:
if item['age'] < 18:
count += 1
print("未成年学生数:", count)
d.打印手机尾号是8的学生的名字
for item in students:
if item['del'][-1] == '8':
print(item['name'])
e.打印最高分和对应的学生的名字
max_scores = students[0]['scores']
for index in students:
if index['scores'] > max_scores:
max_scores = index['scores']
print("最高分为:", max_scores)
for stu_dict in students:
if max_scores == stu_dict['scores']:
print(stu_dict['name'])
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 插入排序
scores = []
for item in students:
scores.append(item['scores'])
for i in range(len(scores)-1):
index = i+1
for j in range(i+1):
if scores[j] > scores[i+1]:
index = j
break
if index >= i+1:
continue
count = scores[i+1]
count1 = students[i+1]
for k in range(i+1,index,-1):
scores[k] = scores[k-1]
students[k] = students[k-1]
scores[index] = count
students[index] = count1
for item in students[::-1]:
print(item)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字
sc_list1 = ['鲁班', '天美', '王昭君', '荆轲']
sc_list2 = ['天美', '夏侯惇', '公孙离', '廉颇', '貂蝉']
sc_list3 = ['天美', '鲁班', '貂蝉', '马可波罗', '夏侯惇', '杨玉环']
sc_set1 = set(sc_list1)
sc_set2 = set(sc_list2)
sc_set3 = set(sc_list3)
a. 求选课学生总共有多少人
sc_stu = sc_set1 | sc_set2 | sc_set3
print('选课总人数:', len(sc_stu))
b. 求只选了第一个学科的人的数量和对应的名字
new_1 = sc_set1 - sc_set2 - sc_set3
print('只选了第一个学科的人的数量:', len(new_1))
for item in new_1:
print(item, end=' ')
c. 求只选了一门学科的学生的数量和对应的名字
new_2 = (sc_set1 ^ sc_set2 ^ sc_set3) - (sc_set1 & sc_set2 & sc_set3)
print("只选了一门学科的学生的数量:", len(new_2))
for item in new_2:
print(item, end=' ')
d. 求只选了两门学科的学生的数量和对应的名字
new_3 = (sc_set1 | sc_set2 | sc_set3) - (sc_set1 - sc_set2 - sc_set3)\
- (sc_set2 - sc_set1 - sc_set3) - (sc_set3 - sc_set2 - sc_set1)\
- (sc_set1 & sc_set2 & sc_set3)
print("只选了两门学科的学生的数量:", len(new_3))
for item in new_3:
print(item, end=' ')
e. 求选了三门学生的学生的数量和对应的名字
new_4 = sc_set1 & sc_set2 & sc_set3
print("选了三门学生的学生的数量", len(new_4))
for item in new_4:
print(item, end=' ')