1.声明一个字典保存一个学生的信息,
学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
dic1 = {"name": "aa", "age": 18, "math": 85, "tel": "785613545466", "gender": "男"}
dic2 = {"name": "bb", "age": 17, "math": 80, "tel": "785613545467", "gender": "女"}
dic3 = {"name": "cc", "age": 19, "math": 98, "tel": "785613545468", "gender": "不明"}
dic4 = {"name": "dd", "age": 20, "math": 59, "tel": "685613545468", "gender": "男"}
dic5 = {"name": "ee", "age": 18, "math": 40, "tel": "785613545463", "gender": "女"}
dic6 = {"name": "ff", "age": 16, "math": 90, "tel": "785613545461", "gender": "不明"}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
stu_list = [
{"name": "aa", "age": 18, "math": 85, "tel": "785613545466", "gender": "男"},
{"name": "bb", "age": 17, "math": 80, "tel": "785613545467", "gender": "女"},
{"name": "cc", "age": 19, "math": 98, "tel": "785613545468", "gender": "不明"},
{"name": "dd", "age": 20, "math": 59, "tel": "685613545468", "gender": "男"},
{"name": "ee", "age": 18, "math": 40, "tel": "785613545463", "gender": "女"},
{"name": "ff", "age": 16, "math": 90, "tel": "785613545461", "gender": "不明"}
]
a.统计不及格学生的个数
count = 0
for student in stu_list:
if student["math"] < 60:
count += 1
pass
else:
continue
pass
pass
print(count)
b.打印不及格学生的名字和对应的成绩
for student in stu_list:
if student["math"] < 60:
print(student["name"], student["math"])
pass
else:
continue
pass
pass
c.统计未成年学生的个数
count = 0
for student in stu_list:
if student["age"] < 18:
count += 1
pass
else:
continue
pass
pass
print(count)
d.打印手机尾号是8的学生的名字
for student in stu_list:
if student["tel"][-1] == "8":
print(student["name"])
pass
else:
continue
pass
pass
e.打印最高分和对应的学生的名字
max_math = 0
name = ''
for student in stu_list:
stu_math = student["math"]
if stu_math > max_math:
max_math = stu_math
name = student['name']
pass
pass
print(name, max_math)
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for i in range(len(stu_list)):
for j in range(len(stu_list)):
if stu_list[i]["math"] > stu_list[j]["math"]:
stu_list[i], stu_list[j] = stu_list[j], stu_list[i]
pass
pass
pass
print(stu_list)
# 方法2
stu_list2 = stu_list
scores = []
# 取出所有成绩
for i in range(len(stu_list2)):
scores.append(stu_list2[i]['math'])
pass
print(scores)
scores.sort(reverse=True)
print(scores)
# 将所有对应成绩存入列表
new_stu_list = []
for score in scores:
for student in stu_list2[:]:
if score == student['math']:
new_stu_list.append(student)
stu_list2.remove(student)
break
pass
pass
pass
print(new_stu_list)
# 选择排序
for student in stu_list[:]:
if student["gender"] == "不明":
stu_list.remove(student)
pass
pass
print(stu_list)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list_math = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
list_chinese = ['g', 'd', 'q', 'h', 'l', 'v']
list_english = ['g', 'b', 'h', 'm', 'e']
set_math = set(list_math)
set_chinese = set(list_chinese)
set_english = set(list_english)
a. 求选课学生总共有多少人
set1 = set_math | set_english | set_chinese
print(len(set1))
b. 求只选了第一个学科的人的数量和对应的名字
set1 = (set_math & (set_chinese ^ set_math ^ set_english)) - (set_math & set_english & set_chinese)
print(len(set1), ' ', set1)
c. 求只选了一门学科的学生的数量和对应的名字
set2 = (set_math ^ set_english ^ set_chinese ) - (set_math & set_english & set_chinese)
print(len(set2), set2)
d. 求只选了两门学科的学生的数量和对应的名字
set1 = set_math | set_english | set_chinese
# 只选了一门学科
set2 = (set_math ^ set_english ^ set_chinese ) - (set_math & set_english & set_chinese)
# 选了三门学科
set3 = set_math & set_english & set_chinese
set4 = set1 - set2 - set3
print(len(set4), set4)
e. 求选了三门学生的学生的数量和对应的名字
set3 = set_math & set_english & set_chinese
print(len(set3), set3)