1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student ={'name': '张三', 'age': 18, 'grade': 89, 'telephone': '154656', 'sex':['男', '女', '不明']}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students1 ={'name': '张三', 'age': 17, 'grade': 89, 'telephone': 154656, 'sex': '不明'}
students2 ={'name': '李四', 'age': 20, 'grade': 66, 'telephone': 156468, 'sex': '女'}
students3 ={'name': '王五', 'age': 19, 'grade': 50, 'telephone': 198798, 'sex': '男'}
students4 ={'name': '小明', 'age': 22, 'grade': 49, 'telephone': 654878, 'sex': '男'}
students5 ={'name': '小红', 'age': 23, 'grade': 78, 'telephone': 133132, 'sex': '女'}
students6 ={'name': '小蓝', 'age': 15, 'grade': 99, 'telephone': 646522, 'sex': '不明'}
students = [students1, students2, students3, students4, students5, students6]
a.统计不及格学生的个数
g = 0
for x in students:
if x['grade'] < 60:
g += 1
print(g)
b.打印不及格学生的名字和对应的成绩
for x in students:
if x['grade'] < 60:
print(x['name'], x['grade'])
c.统计未成年学生的个数
w = 0
for x in students:
if x['age'] < 18:
w += 1
print('未成年的个数是:', w)
d.打印手机尾号是8的学生的名字
for x in students:
if x['telephone'] % 10 == 8:
print(x['name'])
e.打印最高分和对应的学生的名字
cj = []
for x in students:
cj.append(x['grade'])
for y in students:
if max(cj) == y['grade']:
print(y['grade'], y['name'])
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
xs = []
for x in students:
xs.append(x['grade'])
cj1 = sorted(xs, reverse = True)
print(cj1)
px = []
for y in cj1:
for x in students:
if y == x['grade']:
px.append(x)
print(px)
g.删除性别不明的所有学生
for x in students:
if x['sex'] == '不明':
students.remove(x)
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
list1 = ['小明', '小红', '王五', '李雷']
list2 = ['张三', '小红', '小明', '王五']
list3 = ['小红', '小明', '张三', '韩梅梅']
a. 求选课学生总共有多少人
list4 = list1 + list2 + list3
list(dict.fromkeys(list4))
print(len(list(dict.fromkeys(list4))))
print(list(dict.fromkeys(list4)))
b. 求只选了第一个学科的人的数量和对应的名字
one = 0
for x in list1:
if x not in list2 and x not in list3:
one += 1
print(x)
print(one)
c. 求只选了一门学科的学生的数量和对应的名字
shu = 0
for x in list(dict.fromkeys(list4)):
if (x in list1 and x not in list2 and x not in list3) or (x not in list1 and x in list2 and x not in list3) or (x not in list1 and x not in list2 and x in list3):
shu += 1
print(x)
print(shu)
d. 求只选了两门学科的学生的数量和对应的名字
er = 0
for x in list(dict.fromkeys(list4)):
if (x in list1 and x in list2 and x not in list3) or (x in list1 and x not in list2 and x in list3) or (x not in list1 and x in list2 and x in list3):
er += 1
print(x)
print(er)
e. 求选了三门学生的学生的数量和对应的名字
san = 0
for x in list(dict.fromkeys(list4)):
if x in list1 and x in list2 and x in list3:
san += 1
print(x)
print(san)