day7-作业

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

dict1 = {'name': '瑞兹', 'age': 19, 'score': 90, 'phone': 
19988880000, 'gender': 'man'}

list1 = [{'name': '瑞兹', 'age': 25, 'score': 95, 'phone': 19988880000, 'gender': 'man'},
         {'name': '维恩', 'age': 16, 'score': 95, 'phone': 19988888888, 'gender': 'female'},
         {'name': '寒冰', 'age': 15, 'score': 70, 'phone': 19988881111, 'gender': 'female'},

         {'name': '龙女', 'age': 19, 'score': 80, 'phone': 19988882222, 'gender': 'female'},
         {'name': '德莱文', 'age': 18, 'score': 50, 'phone': 19988883333, 'gender': 'unknown'},
         {'name': '嘉文', 'age': 17, 'score': 59, 'phone': 19988885555, 'gender': 'man'}
         ]

1.统计不及格学生的个数

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

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

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

3.统计未成年学生的个数

for stu in list1:
    if stu['age'] < 18:
        count += 1
print('未成年学生个数为:', count)

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

for stu in list1:
    if stu['phone'] % 10 == 8:
        print('手机尾号是8的学生的名字是:', stu['name'])

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

max_score = 0
for stu in list1:
    if stu['score'] > max_score:
        max_score = stu['score']
for stu in list1:
    if max_score == stu['score']:
        print('最高分的学生为:', stu['name'])

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

for stu in list1:
    if stu['gender'] != 'man' and stu['gender'] != 'female':
        list1.remove(stu)
print(list1)

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

chinese = ['寒冰', '嘉文', '瑞兹', '德莱文']
maths = ['希维尔', '瑞兹', '维恩', '寒冰']
english = ['希维尔', '德莱文', '瑞兹']

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

set1 = set(chinese)
set2 = set(maths)
set3 = set(english)
print('选课学生总共有%d人' % len(set1 | set2 | set3), '分别为:', set1 | set2 | set3)

2.求只选了第一个学科的人的数量和对应的名字

print(len(set1 - set2 - set3), set1 - set2 - set3)

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

count = 0
x = ''
y = ''
z = ''
for stu in set1:
    if stu not in set2 and set3:
        x = x + ' ' + stu
        count += 1
for stu in set2:
    if stu not in set1 and set3:
        print(stu)
        y = y + ' ' + stu
        count += 1
for stu in set3:
    if stu not in set1 and set2:
        z = z + ' ' + stu
        count += 1
print('只选了一门学科的学生的数量为:', count, '名字是:', x, y, z)

4.求只选了两门学科的学生的数量和对应的名字

x = ''
y = ''
z = ''
count = 0
for stu in set1:
    if stu in set2 and stu not in set3 or stu in set3 and stu not in set2:
        x = x + ' ' + stu
        count += 1
for stu in set2:
    if stu in set1 and stu not in set3 or stu in set3 and stu not in set1:
        y = y + ' ' + stu
        count += 1
for stu in set3:
    if stu in set1 and stu not in set2 or stu in set2 and stu not in set1:
        z = z + ' ' + stu
        count += 1
print(x, y, z, count)

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

x = list(set(chinese) & set(maths) & set(english))
print(x)
print('===========================================================')
count = 0
for stu in chinese:
    if stu in maths and stu in english:
        print(stu)
        for stu in maths:
            if stu in chinese and stu in english:
                print(stu)
                for stu in english:
                    if stu in chinese and stu in maths:
                         print(stu)
                count += 1
print(count)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    Ed97001阅读 358评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    风月辞寒阅读 215评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    ______n___阅读 169评论 0 0
  • 今天把精保的工具都给打上标签,让客户更清晰的看懂。我方便我门自己干活。
    d005a7da9b80阅读 101评论 0 0
  • Create a color from HSL, and alpha values from 0 to 1.创建一...
    刘板栗阅读 1,080评论 0 0