首页 分享 python列表、字典相关练习题

python列表、字典相关练习题

来源:萌宠菠菠乐园 时间:2025-03-24 14:10

1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)

student={'name':'张三','age':'23','score':88,'tel':'23423532','gender':'男'} 1

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':'女'}, ] 12345678

a.统计不及格学生的个数

count = 0 for i in students: if i['score'] < 60: count += 1 print(count) 12345

b.打印不及格学生的名字和对应的成绩

for i in students: if i['score'] < 60: print(i['name'],i['score']) 123

c.统计未成年学生的个数

count = 0 for i in students: if i['age'] < 18: count += 1 print(count) 12345

d.打印手机尾号是8的学生的名字

count = 0 for i in students: if int(i['tel']) % 10 == 8: print(i['name']) 1234

e.打印最高分和对应的学生的名字

max_score = 0 name = '' for student in students: if student['score'] > max_score: max_score = student['score'] name = student['name'] print(name) 1234567

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 = j students[i], students[num] = students[num], students[i] max_score = 0 print(students) 12345678910

g.删除性别不明的所有学生

for i in students: if i['gender'] == '不明': students.remove(i) print(students) 1234

3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)

chinese = ['小明','小张','小黄','小杨'] math = ['小黄','小李','小王','小杨','小周'] english = ['小杨','小张','小吴','小冯','小周'] 123

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)) 1234567891011

b. 求只选了第一个学科的人的数量和对应的名字

count = 0 for i in chinese: if i not in math and i not in english: count += 1 print(i) print(count) 123456

c. 求只选了一门学科的学生的数量和对应的名字

count = 0 for i in chinese: if i not in math and i not in english: count += 1 print(i) for i in math: if i not in chinese and i not in english: count += 1 print(i) for i in english: if i not in math and i not in chinese: count += 1 print(i) print(count) 123456789101112131415

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) 1234567891011

e. 求选了三门学生的学生的数量和对应的名字

a = set(chinese) b = set(math) c = set(english) d = a & b & c print(d) 12345

看完点个赞呗,谢谢!

相关知识

python 列表转为字典的两个小方法
Python基础练习题‌100道电子版及源码文件
python:列表的练习题
python 练习题
python 元组和字典 基础
Python小练习
Python入门习题大全——宠物
太原理工大学python考试题总结
Python之函数
小菜鸟的python学习之路(7)

网址: python列表、字典相关练习题 https://www.mcbbbk.com/newsview1098183.html

所属分类:萌宠日常
上一篇: 博主与流浪狗的救援之旅,医生救治
下一篇: 流浪狗生死关头,博主挺身而出!医

推荐分享