2019-11-12-作业

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

stu = {'name': '', 'age': 20, 'score': 89, 'tel': '', 'gender': ''}

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

a.统计不及格学生的个数
b.打印不及格学生的名字和对应的成绩
c.统计未成年学生的个数
d.打印手机尾号是8的学生的名字
e.打印最高分和对应的学生的名字
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃) 
g.删除性别不明的所有学生
all_stu = [
    {'name': 'xm', 'age': 22, 'score': 89, 'tel': '124532132', 'gender': '1'},
    {'name': 'xh', 'age': 20, 'score': 60, 'tel': '13548', 'gender': '0'},
    {'name': 'ah', 'age': 16, 'score': 56, 'tel': '6543543', 'gender': '1'},
    {'name': 'lh', 'age': 19, 'score': 45, 'tel': '5463123', 'gender': '0'},
    {'name': 'ls', 'age': 25, 'score': 90, 'tel': '1318', 'gender': '1'},
    {'name': 'zs', 'age': 17, 'score': 95, 'tel': '165443', 'gender': '3'}
]
# a
count = 0
for stu in all_stu:
    if stu['score'] < 60:
        count += 1
print('不及格人数:', count)
# b
for stu in all_stu:
    if stu['score'] < 60:
        name = stu['name']
        score = stu['score']
        print('不及格学生姓名{},分数{}:'.format(name, score))
# c
count = 0
for stu in all_stu:
    if stu['age'] < 18:
        count += 1
print('未成年人数:', count)
# d
for stu in all_stu:
    if stu['tel'][-1] == '8':
        print(stu['name'])
# e

max_s = 0
for stu in all_stu:
    if stu['score'] > max_s:
        max_s = stu['score']
        name = stu['name']
print('最高分{},学生:{}'.format(max_s, name))
# f

for x in range(len(all_stu)):
    for y in range(x + 1, len(all_stu)):
        if all_stu[x]['score'] < all_stu[y]['score']:
            all_stu[x], all_stu[y] = all_stu[y], all_stu[x]
for stu in all_stu:
    print(stu['score'])
# g

for stu in all_stu:
    if stu['gender'] == '3':
        all_stu.remove(stu)
for stu in all_stu:
    print(stu['name'])

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

students1 = ['s1', 's2', 's5']
students2 = ['s1', 's2', 's3', 's6']
students3 = ['s1', 's3', 's4']
# a

all1 = students1+students2+students3
a_stu = set(all1)
count = 0
for stu in a_stu:
    count += 1
print(count)
# b
all2 = students2+students3
a = set(students1) - set(all2)
count = 0
for s in a:
    count += 1
print('人数{},名字{}'.format(count, a))
# c
stu1 = set(students1) & set(students2) & set(students3)
stu2 = set(students1) ^ set(students2) ^ set(students3)
for stu in stu1:
    stu2.remove(stu)
count = 0
for stu in stu2:
    count += 1
print('数量{},名字:{}'.format(count, stu2))
# d
a = set(students1) & set(students2) - set(students3)
b = set(students1) & set(students3) - set(students2)
c = set(students2) & set(students3) - set(students1)
list1 = list(a) + list(b) + list(c)
count = 0
for stu in list1:
    count += 1
print('数量:{},学生:{}'.format(count,list1))
# e
stu1 = set(students1) & set(students2) & set(students3)
count = 0
for stu in stu1:
    count += 1
print('数量{},名字:{}'.format(count, stu1))

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    _二一九Jiu阅读 207评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 2.声明一个列表,在列表中保...
    归墟_a3c1阅读 234评论 0 0
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) 2.声...
    Shyerjouniewin阅读 213评论 0 0
  • 后来,我们都知道,感情的事情是不能强求的,不是你付出了多少,就能换回来多少。 后来,我们都明白,有...
    渡若阅读 372评论 0 0
  • ——木心
    土豆er洋芋马铃薯阅读 54评论 0 0