1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
stu = {
'name': '张三',
'age': 18,
'grade': 98,
'tel': '19654545465',
'sex': '男'}
print(stu)
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
stu = {
'students': [
{
'name': '张三',
'age': 18,
'grade': 98,
'tel': '19654545465',
'sex': '男'
},
{
'name': '李四',
'age': 19,
'grade': 99,
'tel': '19654545468',
'sex': '男'
},
{
'name': '小花',
'age': 17,
'grade': 95,
'tel': '19654545467',
'sex': '女'
},
{
'name': '小明',
'age': 20,
'grade': 48,
'tel': '19655545466',
'sex': '男'
},
{
'name': '小C',
'age': 21,
'grade': 70,
'tel': '19655545465',
'sex': '男'
},
{
'name': '小岚',
'age': 23,
'grade': 58,
'tel': '19655545468',
'sex': '不明'
},
]
}
a.统计不及格学生的个数
count = 0
for i in stu['students']:
if i['grade'] < 60:
count += 1
print(count)
b.打印不及格学生的名字和对应的成绩
for i in stu['students']:
if i['grade'] < 60:
print(i['name'], i['grade'])
c.统计未成年学生的个数
count = 0
for i in stu['students']:
if i['age'] < 18:
count += 1
print(count)
d.打印手机尾号是8的学生的名字
for i in stu['students']:
if str(i['tel'])[-1:-2:-1] == '8':
print(i['name'])
e.打印最高分和对应的学生的名字
max_grade = 0
for i in stu['students']:
if i['grade'] > max_grade:
max_grade = i['grade']
print('最高分:', max_grade)
for name in stu['students']:
if name['grade'] == max_grade:
print('获得最高分的学生:', name['name'])
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
g.删除性别不明的所有学生
for i in stu['students'][:]:
if i['sex'] == '不明':
i.clear()
print(stu)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
subject = {
'计算机网络': ['张三', '李四', '小王'],
'数据结构': ['小王', '小张'],
'Python': ['张三', '小兰']
}
a. 求选课学生总共有多少人
people = []
for i in subject['计算机网络']:
people.append(i)
for j in subject['数据结构']:
people.append(j)
for k in subject['Python']:
people.append(k)
new = []
for i in people:
if i not in new:
new.append(i)
print(len(new))
b. 求只选了第一个学科的人的数量和对应的名字
network = []
structure = []
python = []
count = 0
for i in subject['计算机网络']:
network.append(i)
for j in subject['数据结构']:
structure.append(j)
for k in subject['Python']:
python.append(k)
for i in network:
if i not in structure and i not in python:
count += 1
print(i)
print(count)
c. 求只选了一门学科的学生的数量和对应的名字
network = []
structure = []
python = []
for i in subject['计算机网络']:
network.append(i)
for j in subject['数据结构']:
structure.append(j)
for k in subject['Python']:
python.append(k)
count = 0
for i in network:
if i not in structure and i not in python:
count += 1
print(i)
for j in structure:
if j not in python and j not in network:
count += 1
print(j)
for k in python:
if k not in network and k not in structure:
count += 1
print(k)
print(count)
d. 求只选了两门学科的学生的数量和对应的名字
network = []
structure = []
python = []
for i in subject['计算机网络']:
network.append(i)
for j in subject['数据结构']:
structure.append(j)
for k in subject['Python']:
python.append(k)
count = 0
for i in network:
if i in structure and i not in python or i not in structure and i in python:
count += 1
print(i)
for j in structure:
if j in network and j not in python or j not in network and j in python:
count += 1
print(j)
for k in python:
if k in network and k not in structure or k not in network and k in structure:
count += 1
print(k)
print(count)
e. 求选了三门学生的学生的数量和对应的名字
network = []
structure = []
python = []
count = 0
for i in subject['计算机网络']:
network.append(i)
for j in subject['数据结构']:
structure.append(j)
for k in subject['Python']:
python.append(k)
for i in network:
if i in structure and i in python:
count += 1
print(i)
for j in structure:
if j in network and j in python:
count += 1
print(j)
for k in python:
if k in network and k in structure:
count += 1
print(k)
print(count)
750

被折叠的 条评论
为什么被折叠?



