1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student = {"name": "李喵喵", "age": 18, "score": 89, "tel": "110120119", "gender": "男"}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [{'name': "卡'兹克", 'age': 23, 'score': 87, 'tel': '43256777', 'gender': '不明'},
{'name': '兰博', 'age': 15, 'score': 95, 'tel': '43256756', 'gender': '不明'},
{'name': '佐伊', 'age': 2000, 'score': 50, 'tel': '52463854', 'gender': '女'},
{'name': '瑞兹', 'age': 1500, 'score': 100, 'tel': '110', 'gender': '男'},
{"name": "李喵喵", "age": 18, "score": 89, "tel": "110120119", "gender": "男"},
{'name': '疾风剑豪', 'age': 15, 'score': 95, 'tel': '4325678', 'gender': '男'},
]
a.统计不及格学生的个数
count1 = 0
for student1 in students:
if student1["score"] < 60:
count1 += 1
print(count1)
b.打印不及格学生的名字和对应的成绩
for student2 in students:
if student2["score"] < 60:
print(student2["name"], student2["score"])
c.统计未成年学生的个数
count2 = 0
for student3 in students:
if student3["age"] < 18:
count2 += 1
print(count2)
d.打印手机尾号是8的学生的名字
for student4 in students:
if list(student4["tel"])[-1] == "8":
print(student4["name"])
e.打印最高分和对应的学生的名字
score = []
for student5 in students:
score.append(student5["score"])
print(score)
for student5 in students:
if student5["score"] == max(score):
print("最高分学生:", student5["name"], max(score))
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
for student6 in students:
for x in range(len(students)-1):
if students[x]["score"] < students[x + 1]["score"]:
students[x],students[x + 1] = students[x + 1],students[x]
print(students)
g.删除性别不明的所有学生
for student5 in students[:]:
if student5["gender"] == "不明":
students.remove(student5)
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list1 = ["曹操", "吕布", "张飞", "貂蝉", "小乔"]
list2 = ["大桥", "小乔", "吕子乔", "吕洞宾"]
list3 = ["曹操", "宫本", "貂蝉", "小乔"]
set1 = set(list1)
set2 = set(list2)
set3 = set(list3)
a. 求选课学生总共有多少人
count3 = len((set1 | set2 | set3))
print(count3)
b. 求只选了第一个学科的人的数量和对应的名字
count4 = set1 - set2 - set3
print(count4, len(count4))
c. 求只选了一门学科的学生的数量和对应的名字
count4 = set1 - set2 - set3
count5 = set2 - set1 - set3
count6 = set3 - set2 - set1
s = count5 | count6 | count4
print(s,len(s))
d. 求只选了两门学科的学生的数量和对应的名字
count7 = set1 & set2
count8 = set1 & set3
count9 = set2 & set3
count0 = (count7 | count8 | count9)-(set1 & set2 & set3)
print(count0,len(count0))
e. 求选了三门学生的学生的数量和对应的名字
count11 = set1 & set2 & set3
print(count11,len(count11))