美文网首页
day8-元组、字典、集合作业

day8-元组、字典、集合作业

作者: 冯书简 | 来源:发表于2019-06-13 20:56 被阅读0次

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

student = {'name': '小冯', '年龄': 23, 'score': 66, 'tel': '112'}

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

students = [{'name': '小冯', '年龄': 23, 'score': 66, 'tel': '113'},
            {'name': '小花', '年龄': 22, 'score': 57, 'tel': '114'},
            {'name': '小白', '年龄': 21, 'score': 68, 'tel': '128'},
            {'name': '小洪', '年龄': 11, 'score': 69, 'tel': '116'},
            {'name': '小马', '年龄': 13, 'score': 39, 'tel': '187'},
            {'name': '小二', '年龄': 20, 'score': 66, 'tel': '118'}
]

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

count_low_socre = 0
for student in students:
    if student['score'] < 60:
        count_low_socre += 1        
print(count_low_socre)

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

for student in students:
    if student['score'] < 60:
        print(student['name'], student['score'])

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

count_minor_student  = 0
for student in students:
    if student['年龄'] < 18:
        count_minor_student += 1
print(count_minor_student)

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

for student in students:
    if student['tel'][-1] == '8':
        print(student['name'])

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

max_score = 0
max_score_name = ''
for student in students:
    score = student['score']
    if score > max_score:
        max_score = score
        
print(max_score)
for student in students:
    if student['score'] == max_score:
    print(student['name'])

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


#按照顺序取出分数
for i in range(len(students)-1):
    #每个循环中比较数
    for j in range(len(students)-i-1):
        if students[j]['score'] < students[j + 1]['score']:
            students[j]['score'], students[j + 1]['score'] = students[j + 1]['score'], students[j]['score']
print(students)

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

science = ['小冯', '小花', '小马', '小张']
english = ['小冯', '如花', '小红','小张','小龙']
chemistry = ['小冯', '小光', '小李']
set_science = set(science)
set_english = set(english)
set_chemistry = set(chemistry)

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

count = 0
for _ in (set_science | set_chemistry | set_english):
    count += 1
print(count)
b. 求只选了第一个学科的人的数量和对应的名字
count = 0
for _ in (set_science - set_english - set_chemistry):
    count += 1
print(count, set_science - set_english - set_chemistry)

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

count = 0
for _ in ((set_science ^ set_english ^ set_chemistry) - (set_english & set_chemistry & set_science)):
    count += 1
print(count, ((set_science ^ set_english ^ set_chemistry) - (set_english & set_chemistry & set_science)))
d. 求只选了两门学科的学生的数量和对应的名字
count = 0
for _ in (set_science & set_english - set_chemistry)|( set_english & set_chemistry\
- set_science )|(set_chemistry & set_science - set_english  ):
    count += 1
print(count, (set_science & set_english - set_chemistry)|( set_english & set_chemistry\
- set_science )|(set_chemistry & set_science - set_english  ))

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

count = 0
for _ in (set_science & set_english & set_chemistry):
    count += 1
print(count, set_science & set_english & set_chemistry)

相关文章

网友评论

      本文标题:day8-元组、字典、集合作业

      本文链接:https://www.haomeiwen.com/subject/qcvefctx.html