1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student = {'name':'小明','age':20,'grade':93,'phone':'13888888888','sex':'男'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [
{'name': '小明', 'age': 20, 'grade': 93, 'phone': '13888888888', 'sex': '男'},
{'name': '小新', 'age': 16, 'grade': 58, 'phone': '13877777777', 'sex': '不明'},
{'name': '小红', 'age': 29, 'grade': 82, 'phone': '13866666666', 'sex': '女'},
{'name': '小花', 'age': 20, 'grade': 55, 'phone': '13855555555', 'sex': '女'},
{'name': '小李', 'age':17 'grade': 78, 'phone': '13844444444', 'sex': '不明'},
{'name': '小张', 'age': 25, 'grade': 60, 'phone': '13833333333', 'sex': '男'}
]
a.统计不及格学生的个数
count = 0
for x in students:
if x['grade'] < 60:
count+=1
print('不及格有',count) #2
b.打印不及格学生的名字和对应的成绩
for x in students:
if x['grade'] < 60:
print(x['name'],x['grade']) #小新 58 小花 55
c.统计未成年学生的个数
count = 0
for x in students:
if x['age'] < 18:
count+=1
print('未成年个数是:',count) #2
d.打印手机尾号是8的学生的名字
for x in students:
if int(x['phone'][len(x['phone'])-1])== 8:
print(x['name']) #小明
e.打印最高分和对应的学生的名字
bigfen = 0
name = ''
for x in students:
if int(x['grade'])>bigfen:
bigfen = int(x['grade'])
name = x['name']
print(bigfen,name) #93 小明
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
g.删除性别不明的所有学生
for x in students:
if x['sex'] == '不明':
students.remove(x)
print(students) #[{'name': '小明', 'age': 20, 'grade': 93, 'phone': '13888888888', 'sex': '男'}, {'name': '小红', 'age': 29, 'grade': 82, 'phone': '13866666666', 'sex': '女'}, {'name': '小花', 'age': 20, 'grade': 55, 'phone': '13855555555', 'sex': '女'}, {'name': '小张', 'age': 25, 'grade': 60, 'phone': '13833333333', 'sex': '男'}]
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
chinese = ['小新','小米','小明','小画','小黄']
math = ['小李','小张','小米','小黄']
english=['小明','小黄']
set1 = set(chinese)
set2 = set(math)
set3 = set(english)
sum = (set1 | set2 | set3)
count = 0
for _ in sum:
count+=1
print(count,'人') #7人
b. 求只选了第一个学科的人的数量和对应的名字
chinese = ['小新','小米','小明','小画','小黄']
math = ['小李','小张','小米','小黄']
english=['小明','小黄']
count = 0
for x in chinese:
print(x,end=' ')
count+=1
print(count,'人') #小新 小米 小明 小画 小黄 5 人
c. 求只选了一门学科的学生的数量和对应的名字
chinese = ['小新','小米','小明','小画','小黄']
math = ['小李','小张','小米','小黄']
english=['小明','小黄']
set1 = set(chinese)
set2 = set(math)
set3 = set(english)
set4 = set1 & set2
set5 = set2 & set3
set6 = set1 & set3
set7 = set1 | set2 | set3
set8 = set7 ^ set4 ^ set5 ^ set6
count = 0
for x in set8:
print(x,end=' ')
count+=1
print(count) #小李 小新 小张 小画 4
d. 求只选了两门学科的学生的数量和对应的名字
chinese = ['小新','小米','小明','小画','小黄']
math = ['小李','小张','小米','小黄']
english=['小明','小黄']
set1 = set(chinese)
set2 = set(math)
set3 = set(english)
set4 = set1 & set2
set5 = set1 & set3
set6 = set2 & set3
set7 = set4 | set5 |set6
set8 = set4 & set5 &set6
set9 = set7 - set8
count =0
for x in set9:
print(x,end=' ')
count+=1
print(count) #小米 小明 2
e. 求选了三门学生的学生的数量和对应的名字
chinese = ['小新','小米','小明','小画','小黄']
math = ['小李','小张','小米','小黄']
english=['小明','小黄']
set1 = set(chinese)
set2 = set(math)
set3 = set(english)
set4 = set1 & set2 & set3
count = 0
for x in set4:
print(x,end=' ')
count+=1
print(count) #小黄 1