python列表、字典相关练习题
1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student={'name':'张三','age':'23','score':88,'tel':'23423532','gender':'男'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [{'name':'张三','age':23,'score':88,'tel':'23423532','gender':'男'},{'name':'李四','age':26,'score':80,'tel':'12533453','gender':'女'},{'name':'王五','age':15,'score':58,'tel':'56453453','gender':'男'},{'name':'赵六','age':16,'score':57,'tel':'86786785','gender':'不明'},{'name':'小明','age':18,'score':98,'tel':'23434656','gender':'女'},{'name':'小红','age':23,'score':72,'tel':'67867868','gender':'女'},
]
a.统计不及格学生的个数
count = 0
for i in students:if i['score'] < 60:count += 1
print(count)
b.打印不及格学生的名字和对应的成绩
for i in students:if i['score'] < 60:print(i['name'],i['score'])
c.统计未成年学生的个数
count = 0
for i in students:if i['age'] < 18:count += 1
print(count)
d.打印手机尾号是8的学生的名字
count = 0
for i in students:if int(i['tel']) % 10 == 8:print(i['name'])
e.打印最高分和对应的学生的名字
max_score = 0
name = ''
for student in students:if student['score'] > max_score:max_score = student['score']name = student['name']
print(name)
f.将列表按学生成绩从大到小排序
max_score = students[0].get('score')
num = 0
for i in range(0,len(students)):for j in range(i, len(students)):if students[j].get('score') > max_score:max_score = students[j].get('score')num = jstudents[i], students[num] = students[num], students[i]max_score = 0
print(students)
g.删除性别不明的所有学生
for i in students:if i['gender'] == '不明':students.remove(i)
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = ['小明','小张','小黄','小杨']
math = ['小黄','小李','小王','小杨','小周']
english = ['小杨','小张','小吴','小冯','小周']
a. 求选课学生总共有多少人
a = []
for i in chinese:if i not in a:a.append(i)
for i in math:if i not in a:a.append(i)
for i in english:if i not in a:a.append(i)
print(len(a))
b. 求只选了第一个学科的人的数量和对应的名字
count = 0
for i in chinese:if i not in math and i not in english:count += 1print(i)
print(count)
c. 求只选了一门学科的学生的数量和对应的名字
count = 0
for i in chinese:if i not in math and i not in english:count += 1print(i)
for i in math:if i not in chinese and i not in english:count += 1print(i)
for i in english:if i not in math and i not in chinese:count += 1print(i)
print(count)
d. 求只选了两门学科的学生的数量和对应的名字
chinese = ['小明','小张','小黄','小杨']
math = ['小黄','小李','小王','小杨','小周']
english = ['小杨','小张','小吴','小冯','小周']
c = set(chinese)
m = set(math)
e = set(english)
d = c & m
f = m & e
g = c & e
n = (d ^ f) | (d ^ g)
print(n)
e. 求选了三门学生的学生的数量和对应的名字
a = set(chinese)
b = set(math)
c = set(english)
d = a & b & c
print(d)
看完点个赞呗,谢谢!
python列表、字典相关练习题
1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
student={'name':'张三','age':'23','score':88,'tel':'23423532','gender':'男'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
students = [{'name':'张三','age':23,'score':88,'tel':'23423532','gender':'男'},{'name':'李四','age':26,'score':80,'tel':'12533453','gender':'女'},{'name':'王五','age':15,'score':58,'tel':'56453453','gender':'男'},{'name':'赵六','age':16,'score':57,'tel':'86786785','gender':'不明'},{'name':'小明','age':18,'score':98,'tel':'23434656','gender':'女'},{'name':'小红','age':23,'score':72,'tel':'67867868','gender':'女'},
]
a.统计不及格学生的个数
count = 0
for i in students:if i['score'] < 60:count += 1
print(count)
b.打印不及格学生的名字和对应的成绩
for i in students:if i['score'] < 60:print(i['name'],i['score'])
c.统计未成年学生的个数
count = 0
for i in students:if i['age'] < 18:count += 1
print(count)
d.打印手机尾号是8的学生的名字
count = 0
for i in students:if int(i['tel']) % 10 == 8:print(i['name'])
e.打印最高分和对应的学生的名字
max_score = 0
name = ''
for student in students:if student['score'] > max_score:max_score = student['score']name = student['name']
print(name)
f.将列表按学生成绩从大到小排序
max_score = students[0].get('score')
num = 0
for i in range(0,len(students)):for j in range(i, len(students)):if students[j].get('score') > max_score:max_score = students[j].get('score')num = jstudents[i], students[num] = students[num], students[i]max_score = 0
print(students)
g.删除性别不明的所有学生
for i in students:if i['gender'] == '不明':students.remove(i)
print(students)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
chinese = ['小明','小张','小黄','小杨']
math = ['小黄','小李','小王','小杨','小周']
english = ['小杨','小张','小吴','小冯','小周']
a. 求选课学生总共有多少人
a = []
for i in chinese:if i not in a:a.append(i)
for i in math:if i not in a:a.append(i)
for i in english:if i not in a:a.append(i)
print(len(a))
b. 求只选了第一个学科的人的数量和对应的名字
count = 0
for i in chinese:if i not in math and i not in english:count += 1print(i)
print(count)
c. 求只选了一门学科的学生的数量和对应的名字
count = 0
for i in chinese:if i not in math and i not in english:count += 1print(i)
for i in math:if i not in chinese and i not in english:count += 1print(i)
for i in english:if i not in math and i not in chinese:count += 1print(i)
print(count)
d. 求只选了两门学科的学生的数量和对应的名字
chinese = ['小明','小张','小黄','小杨']
math = ['小黄','小李','小王','小杨','小周']
english = ['小杨','小张','小吴','小冯','小周']
c = set(chinese)
m = set(math)
e = set(english)
d = c & m
f = m & e
g = c & e
n = (d ^ f) | (d ^ g)
print(n)
e. 求选了三门学生的学生的数量和对应的名字
a = set(chinese)
b = set(math)
c = set(english)
d = a & b & c
print(d)
看完点个赞呗,谢谢!
发布评论