day7-作业

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

student = {'name': '张三', 'age': 17, 'score': 78, 'tel': '18628762567', 'gender': '男'}

2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
g.删除性别不明的所有学生

students = [
    {'name': '金角', 'age': 17, 'score': 78, 'tel': '18628762567', 'gender': '男'},
    {'name': '银角', 'age': 18, 'score': 95, 'tel': '15269056798', 'gender': '女'},
    {'name': '奔波霸', 'age': 15, 'score': 50, 'tel': '15315679802', 'gender': '女'},
    {'name': '霸波奔', 'age': 19, 'score': 59, 'tel': '18112831669', 'gender': '男'},
    {'name': '九头虫', 'age': 20, 'score': 80, 'tel': '18412341678', 'gender': '不明'},
    {'name': '牛魔王', 'age': 16, 'score': 88, 'tel': '18917687906', 'gender': '男'}
]
# a)
total_flunk = 0
for student in students:
    if student['score'] < 60:
        total_flunk += 1
print('不及格学生总数为', total_flunk)   # 不及格学生总数为 2

# b)
for student in students:
    if student['score'] < 60:
        print(student['name'], student['score'])   # 奔波霸 50     霸波奔 59

# c)
total_nonage = 0
for student in students:
    if student['age'] < 18:
        total_nonage += 1
print('未成年人数为',  total_nonage)  # 未成年人数为 3

# d)
for student in students:
    if student['tel'][-1] == '8':
        print(student['name'])  # 银角  九头虫

# e)
max_score = 0
max_score_name = ''
for student in students:
    if student['score'] > max_score:
        max_score = student['score']
        max_score_name = student['name']
print(max_score, max_score_name)  # 95  银角

# f)
new_students = students[:]
students.clear()
while True:
    max_score = new_students[0]['score']
    for student in new_students:
        if student['score'] > max_score:
            max_score = student['score']
    for student in new_students:
        if student['score'] == max_score:
            students.append(student)
            new_students.remove(student)
    if len(new_students) == 0:
        break
print(students)
'''
[
{'name': '银角', 'age': 18, 'score': 95, 'tel': '15269056798', 'gender': '女'},
{'name': '牛魔王', 'age': 16, 'score': 88, 'tel': '18917687906', 'gender': '男'},
{'name': '九头虫', 'age': 20, 'score': 80, 'tel': '18412341678', 'gender': '不明'},
{'name': '金角', 'age': 17, 'score': 78, 'tel': '18628762567', 'gender': '男'},
{'name': '霸波奔', 'age': 19, 'score': 59, 'tel': '18112831669', 'gender': '男'},
{'name': '奔波霸', 'age': 15, 'score': 50, 'tel': '15315679802', 'gender': '女'}
]
'''

# g)
for student in students[:]:
    if student['gender'] == '不明':
        students.remove(student)
print(students)
'''
[
{'name': '银角', 'age': 18, 'score': 95, 'tel': '15269056798', 'gender': '女'},
{'name': '牛魔王', 'age': 16, 'score': 88, 'tel': '18917687906', 'gender': '男'},
{'name': '金角', 'age': 17, 'score': 78, 'tel': '18628762567', 'gender': '男'},
{'name': '霸波奔', 'age': 19, 'score': 59, 'tel': '18112831669', 'gender': '男'},
{'name': '奔波霸', 'age': 15, 'score': 50, 'tel': '15315679802', 'gender': '女'}
]
'''

3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
a. 求选课学生总共有多少人
b. 求只选了第一个学科的人的数量和对应的名字
c. 求只选了一门学科的学生的数量和对应的名字
d. 求只选了两门学科的学生的数量和对应的名字
e. 求选了三门学生的学生的数量和对应的名字

chinese = ['哪吒', '杨戬', '雷震子', '琵琶精', '姜子牙', '哮天犬', '土行孙']
math = ['哪吒', '纣王', '姬发', '李靖', '姜子牙', '小猪熊', '木吒', '石矶']
english = ['哪吒', '杨戬', '太乙真人', '妲己', '金吒', '申公豹', '土行孙']

# a)
print('选课学生总共有%d人' % len(set(chinese) | set(math) | set(english)))  # 选课学生总共有17人
# b)
only_chinese = set(chinese) - set(math) - set(english)
print('只选第一门学科的人数有%d人' % len(only_chinese))
print('他们是:')
for name in only_chinese:
    print(name, end=' ')
'''
只选第一门学科的人数有3人
他们是:
琵琶精 雷震子 哮天犬  
'''
print(' ')
# c)
only_math = set(math) - set(chinese) - set(english)
only_english = set(english) - set(chinese) - set(math)
print('只选了一门学科的有%d人' % (len(only_chinese) + len(only_english) + len(only_math)))
print('他们是:')
for name in only_chinese:
    print(name, end=' ')
for name in only_math:
    print(name, end=' ')
for name in only_english:
    print(name, end=' ')
'''
只选了一门学科的有13人
他们是:
琵琶精 雷震子 哮天犬 石矶 小猪熊 木吒 纣王 姬发 李靖 申公豹 太乙真人 金吒 妲己 
'''
# d)
names = set(chinese) | set(math) | set(english)
print(names)
count = 0
for name in names:
    if (name in chinese and name in math and name not in english) or\
       (name in chinese and name in english and name not in math)or\
       (name in english and name in math and name not in chinese):
        print(name)
        count += 1
print('只选了两门学科的有%d人' % count)
'''
姜子牙
杨戬
土行孙
只选了两门学科的有3人
'''

# e)

all_choice = set(english) & set(chinese) & set(math)
print('选了三门的有%d人' % len(all_choice))
for name in all_choice:
    print(name)
'''
选了三门的有1人
哪吒
'''

你可能感兴趣的:(day7-作业)