python列表、字典相关练习题

编程入门 行业动态 更新时间:2024-10-25 12:29:25

python列表、字典相关<a href=https://www.elefans.com/category/jswz/34/1768594.html style=练习题"/>

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列表、字典相关练习题

本文发布于:2024-02-12 02:06:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1685150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:练习题   字典   列表   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!