2019-07-30

作业

1. 声明一个字典保存一个学生的信息,学生信息中包括:

姓名、年龄、成绩(单科)、电话、性别(男、女、不明)

student = {'name': '张三', 'age': 18, 'chinese': '90', 'tel': '13998565868', 'sex': '男'}

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

students = [
        {'name': '张三', 'age': 18, 'chinese': 75, 'tel': '13998565868', 'sex': '男'},
        {'name': '李四', 'age': 18, 'chinese': 60, 'tel': '15866882822', 'sex': '不明'},
        {'name': '小红', 'age': 17, 'chinese': 80, 'tel': '15862812948', 'sex': '女'},
        {'name': '小七', 'age': 19, 'chinese': 90, 'tel': '14486329756', 'sex': '女'},
        {'name': '王五', 'age': 17, 'chinese': 40, 'tel': '17899282227', 'sex': '男'},
        {'name': '小敏', 'age': 19, 'chinese': 50, 'tel': '17859582692', 'sex': '女'},
]
a.统计不及格学生的个数
num_score = 0
for student in students:
    if student["chinese"] < 60:
        num_score += 1
print(num_score)
b.打印不及格学生的名字和对应的成绩
for student in students:
    if student["chinese"] < 60:
        print(student["name"], student["chinese"])
c.统计未成年学生的个数
num_age = 0
for student in students:
    if student["age"] < 18:
        num_age += 1
print(num_age)
d.打印手机尾号是8的学生的名字
for student in students:
    if student["tel"][-1] == "8":
        print(student["name"])
e.打印最高分和对应的学生的名字
score_max = 0
name1 = 0
for student in students:
    if student["chinese"] > score_max:
        score_max = student["chinese"]
        name1 = student["name"]
print(score_max, name1)
f.将列表按学生成绩从大到小排序
student_score = []
student_sort = []

# 将学生成绩遍历出来形成列表,并按照降序排列

for student in students:
    student_score.append(student["chinese"])
# print(student_score)
student_score.sort(reverse=True)
print(student_score)

# 原列表中字典内的成绩与排序后的列表进行比较,
# 如果一样,就按照规定的下标插入新列表,达到排序效果
new_student = []
for score in student_score:
    for student in students[:]:
        if student["chinese"] == score:
            new_student.append(student)
            students.remove(student)
            break
print(new_student)
g.删除性别不明的所有学生
for student in students:
    if student["sex"] == "不明":
        students.remove(student)
print(students)

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

elective_one = ['张三', '李四', '王五', '小明', '小敏', 'Tony', '小赵']
elective_two = ['赵四', '小敏', '小张', 'Tony', '张三', '王五', '李四']
elective_three = ['小林', 'Bob', '小明', 'Tony', '赵四', '李四']
a. 求选课学生总共有多少人
利用集合求并集自动去重的特性
student_all = list(set(elective_one) | set(elective_two) | set(elective_three))
print(len(student_all))
b. 求只选了第一个学科的人的数量和对应的名字
# 第一种
student_only_1 = []
num_1 = 0
for student in elective_one:
    if student not in elective_two and student not in elective_three:
        student_only_1.append(student)
        num_1 += 1
print(num_1, student_only_1)

# 第二种
# 利用集合求差集
student_only_1 = list((set(elective_one) - set(elective_two)) - set(elective_three))
print(len(student_only_1), student_only_1)
c. 求只选了一门学科的学生的数量和对应的名字
# 如上,上面求的是选了第一个学科,可以重复上面的路数,求出单独选了三课的学生数量及名字

# 只选了第二门课的学生

student_only_2 = list((set(elective_two) - set(elective_one)) - set(elective_three))
# print(len(student_only_2), student_only_2)

# 只选了第三门课的学生

student_only_3 = list((set(elective_three) - set(elective_one)) - set(elective_two))
# print(len(student_only_3), student_only_3)

choice_only_num = len(student_only_1) + len(student_only_2) + len(student_only_3)
choice_only_name = student_only_1 + student_only_2 + student_only_3
print(choice_only_num,  choice_only_name)
d. 求只选了两门学科的学生的数量和对应的名字
# 思路: 先求出同时选了两门以及两门以上的学生名单,在求出同时选了三门学生的名单 做减法运算
# 同时选两门及两门以上的
choice_two_and_three = list(set(elective_one) & set(elective_two)) +\
                       list(set(elective_one) & set(elective_three)) +\
                       list(set(elective_two) & set(elective_three))
# print(choice_two_and_three)
# 去重
choice_new_two = []
for name in choice_two_and_three:
    if name not in choice_new_two:
        choice_new_two.append(name)
print(choice_new_two)

# 同时选三门的
choice_three = list((set(elective_one) & set(elective_two) & set(elective_three)))
print(choice_three)


# 只选两门的有
choice_two = list(set(choice_new_two) - set(choice_three))
print(choice_two)
e. 求选了三门学生的学生的数量和对应的名字
choice_three = list((set(elective_one) & set(elective_two) & set(elective_three)))
choice_three_num = len(choice_three)
print(choice_three_num,choice_three)

你可能感兴趣的:(2019-07-30)