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人
哪吒
'''

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    风月辞寒阅读 240评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    Ed97001阅读 383评论 0 0
  • 1.前提:消费者不傻,愿意花钱免广告。 传统营销正面临挑战:理性营销(三段论)、感性营销(激起观众情绪) 2.人类...
    文刀凯阅读 317评论 0 0
  • 夜寂寥 月高悬 卷帘凭窗望伊还 长夜漫漫 伊人渊源 念 念 念 杨柳枝 细雨绵 枯藤老树野花残 望雨听轩 肝肠寸断...
    青梅煮酒论英雄阅读 238评论 0 0
  • 日复一日 年复一年 我们永远铭记甲午海战的硝烟 勿忘国耻 振兴中华 见证着历史的变迁 千年一叹 祖国巨变...
    失心爱阅读 346评论 0 1

友情链接更多精彩内容