day6-字典作业

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

student_message = {
    
    
    'name': 'faker', 
    'age': '30', 
    'score': 99, 
    'tel': '13488668866', 
    'gender': 'man'
}

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)

student_message = [
    {
    
    'name': 'faker',
     'age': 17,
     'score': 88,
     'tel': '13488668866',
     'gender': 'man'},
    {
    
    'name': 'imp',
     'age': 21,
     'score': 59,
     'tel': '13488661234',
     'gender': 'unknown'},
    {
    
    'name': 'uzi',
     'age': 30,
     'score': 58,
     'tel': '13488664321',
     'gender': 'unknown'},
    {
    
    'name': 'IU',
     'age': 27,
     'score': 100,
     'tel': '13488664321',
     'gender': 'woman'},
    {
    
    'name': 'Jay Park',
     'age': 33,
     'score': 100,
     'tel': '12288663458',
     'gender': 'man'},
    {
    
    'name': 'Dok2',
     'age': 35,
     'score': 100,
     'tel': '12288665678',
     'gender': 'man'}

]

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

count_fail = 0
for stu in student_message:
    if stu['score'] < 60:
        count_fail += 1
print(count_fail)

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

for stu in student_message:
    score_fail = stu['score']
    if score_fail < 60:
        print(stu['name'], score_fail)

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

count_nonage = 0
for stu in student_message:
    if stu['age'] < 18:
        count_nonage += 1
print(count_nonage)

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

for stu in student_message:
    if int(stu['tel']) % 10 == 8:
        print(stu['name'])

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

max_score = 0
for stu in student_message:
    stu_score = stu['score']
    if stu_score > max_score:
        max_score = stu_score
for stu in student_message:
    if stu['score'] == max_score:
        print(stu['name'])

​ f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

score_reverse = []
new_stu = []
for stu in student_message:
    if stu['score'] not in score_reverse:
        score_reverse.append(stu['score'])
score_reverse.sort(reverse=True)
for x in score_reverse:
    for y in student_message:
        if x == y['score']:
            new_stu.append(y)
print(new_stu)

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

for stu in student_message[:]:
    if stu['gender'] == 'unknown':
        student_message.remove(stu)
print(student_message)

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

subject1 = ['a', 'b', 'c', 'd']
subject2 = ['e', 'f', 'a']
subject3 = ['a', 'd', 'e', 'g', 'h']

​ a. 求选课学生总共有多少人

stu = []
for name in subject1:
    if name not in stu:
        stu.append(name)
for name in subject2:
    if name not in stu:
        stu.append(name)
for name in subject3:
    if name not in stu:
        stu.append(name)
print(len(stu))
b. 求只选了第一个学科的人的数量和对应的名字
print('共', len(subject1), '人,名字:', subject1[:])

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

count = 0
name_list = []
for name in subject1:
    if name not in subject2 and name not in subject3:
        count += 1
        name_list.append(name)
for name in subject2:
    if name not in subject1 and name not in subject3:
        count += 1
        name_list.append(name)
for name in subject3:
    if name not in subject2 and name not in subject1:
        count += 1
        name_list.append(name)
print('只选了一门学科的学生有', count, '个,名字是:', name_list, sep='')
d. 求只选了两门学科的学生的数量和对应的名字
name_list = []
for name in subject1:
    if name in subject2 and name not in subject3 or name not in subject2 and name in subject3:
        name_list.append(name)
for name in subject2:
    if name in subject1 and name not in subject3 or name not in subject1 and name in subject3:
        name_list.append(name)
for name in subject3:
    if name in subject1 and name not in subject2 or name not in subject1 and name in subject2:
        name_list.append(name)
name_list1 = []
for name in name_list:
    if name not in name_list1:
        name_list1.append(name)
print('只选了两门学科的学生有', len(name_list1), '个,名字是:', name_list1, sep='')

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

three = []
for name1 in subject1:
    for name2 in subject2:
        for name3 in subject3:
            if name1 == name2 == name3:
                three.append(name1)
print('选了三门学科的学生有', len(three), '个,名字是:', three, sep='')

猜你喜欢

转载自blog.csdn.net/xdhmanan/article/details/108860120