2019-07-30_Work_Day7

1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明)
    student = {'name': 'Jack', 'age': 19, 'Chinese': 87, 'Math': 67, 'English': 78, 'tel': '13012454622',
               'gender': 'male'}
2.声明一个列表,在列表中保存6个学生的信息(6个题1中的字典)
    students = [
        {'name': 'Jack', 'age': 19, 'Chinese': 87, 'Math': 67, 'English': 78, 'tel': '13012454622',
         'gender': 'male'},
        {'name': 'Tom', 'age': 18, 'Chinese': 75, 'Math': 71, 'English': 90, 'tel': '15895254728',
         'gender': 'male'},
        {'name': 'Mela', 'age': 20, 'Chinese': 58, 'Math': 80, 'English': 78, 'tel': '15102044086',
         'gender': 'female'},
        {'name': 'Linda', 'age': 17, 'Chinese': 80, 'Math': 90, 'English': 55, 'tel': '13905224387',
         'gender': 'female'},
        {'name': 'Tim', 'age': 19, 'Chinese': 67, 'Math': 76, 'English': 78, 'tel': '131052353547',
         'gender': 'male'},
        {'name': 'Tony', 'age': 18, 'Chinese': 85, 'Math': 59, 'English': 89, 'tel': '18434529020',
         'gender': 'unknown'}
    ]
a.统计不及格学生的个数
def count_failed(stus):
    """统计不及格学生的个数"""
    failed_stu = [stu for stu in stus if stu['Chinese'] < 60 or stu['Math'] < 60 or stu['English'] < 60]
    return len(failed_stu)
b.打印不及格学生的名字和对应的成绩
def failed_info(stus):
    """打印不及格学生的名字和对应的成绩"""
    for stu in stus:
        if stu['Chinese'] < 60 or stu['Math'] < 60 or stu['English'] < 60:
            print('name:', stu['name'], end=" ")
            if stu['Chinese'] < 60:
                print('Chinese:', stu['Chinese'])
            elif stu['Math'] < 60:
                print('Math:', stu['Math'])
            else:
                print('English:', stu['English'])
    # failed_stu = [
    #   {'name': stu['name'], 'Chinese': stu['Chinese']} if stu['Chinese'] < 60 else {'name': stu['name'],
    #                                                                                 'Math': stu['Math']} if stu[
    #                                                                                                             'Math'] < 60 else {
    #       'name': stu['name'], 'English': stu['English']}
    #   for stu in stus if stu['Chinese'] < 60 or stu['Math'] < 60 or stu['English'] < 60]
    # return failed_stu
c.统计未成年学生的个数
def count_not_adult(stus):
    """统计未成年学生的个数"""
    return len([stu for stu in stus if stu['age'] < 18])
d.打印手机尾号是8的学生的名字
def select_tel(stus):
    """打印手机尾号是8的学生的名字"""
    return [stu['name'] for stu in stus if stu['tel'][-1] == '8']
e.打印最高分和对应的学生的名字
def max_score(stus):
    """打印最高分和对应的学生的名字"""
    max_ch = max(stus, key=lambda stu: stu['Chinese'])
    max_math = max(stus, key=lambda stu: stu['Math'])
    math_eng = max(stus, key=lambda stu: stu['English'])
    max_stu = [
        {'name': max_ch['name'], 'Chinese': max_ch['Chinese']},
        {'name': max_math['name'], 'Math': max_math['Math']},
        {'name': math_eng['name'], 'English': math_eng['English']},
    ]
    return max_stu
f.将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
def sort_stu(stus):
    """将列表按学生成绩从大到小排序"""
    # from operator import itemgetter
    # stus.sort(key=(itemgetter('Chinese', 'Math', 'English')), reverse=True)
    # # stus = sorted(stus, key=(itemgetter('Chinese', 'Math', 'English')))[::-1]
    # for index in range(len(stus)):
    #   print(stus[index])

    scores = []
    for stu in stus:
        scores.append(stu['Chinese'] + stu['Math'] + stu['English'])
    s_len = len(scores)
    for i in range(s_len - 1):
        for j in range(s_len - i - 1):
            if scores[j] < scores[j + 1]:
                scores[j], scores[j + 1] = scores[j + 1], scores[j]
                stus[j], stus[j + 1] = stus[j + 1], stus[j]
    for index in range(s_len):
        print(scores[index], stus[index])
g.删除性别不明的所有学生
def del_no_gender(stus):
    """删除性别不明的所有学生"""
    for stu in stus[:]:
        if stu['gender'] == 'unknown':
            stus.remove(stu)
    for stu in stus:
        print(stu)
3.用三个列表表示三门学科的选课学生姓名(一个学生可以同时选多门课)
    chinese = ['Jack', 'Tim', 'Mela', 'Linda']
    math = ['Linda', 'Mela', 'Tony']
    english = ['Tom', 'Tony', 'Linda']
a. 求选课学生总共有多少人
def count_all_stu(list1, list2, list3):
    """求选课学生总共有多少人"""
    return len(list(set(list1) | set(list2) | set(list3)))
b. 求只选了第一个学科的人的数量和对应的名字
def first_course_info(list1, list2, list3):
    """求只选了第一个学科的人的数量和对应的名字"""
    first_course = list(set(list1) - set(list2) - set(list3))
    return len(first_course), first_course
c. 求只选了一门学科的学生的数量和对应的名字
def choose_one(list1, list2, list3):
    """求只选了一门学科的学生的数量和对应的名字"""
    first_course = list(set(list1) - set(list2) - set(list3))
    second_course = list(set(list2) - set(list1) - set(list3))
    third_course = list(set(list3) - set(list1) - set(list2))

    first_course = {'subject': 'Chinese', 'num': len(first_course), 'name': tuple(first_course)}
    second_course = {'subject': 'Math', 'num': len(second_course), 'name': tuple(second_course)}
    third_course = {'subject': 'English', 'num': len(third_course), 'name': tuple(third_course)}
    ch_one_list = [first_course, second_course, third_course]
    return ch_one_list
d. 求只选了两门学科的学生的数量和对应的名字
def choose_two(list1, list2, list3):
    """求只选了两门学科的学生的数量和对应的名字"""
    first_second = list(set(list1) & set(list2) - set(list3))
    first_third = list(set(list1) & set(list3) - set(list2))
    second_third = list(set(list2) & set(list3) - set(list1))
    first_second = {'subject': 'Chinese & Math', 'num': len(first_second), 'name': tuple(first_second)}
    first_third = {'subject': 'Chinese & English', 'num': len(first_third), 'name': tuple(first_third)}
    second_third = {'subject': 'Math & English', 'num': len(second_third), 'name': tuple(second_third)}
    ch_two_list = [first_second, first_third, second_third]
    for stu in ch_two_list:
        print(stu)
e. 求选了三门学生的学生的数量和对应的名字
def choose_three(list1, list2, list3):
    """求选了三门学生的学生的数量和对应的名字"""
    choose_list = list(set(list1) & set(list2) & set(list3))
    choose_list = {'subject': 'Chinese & Math & English', 'num': len(choose_list), 'name': tuple(choose_list)}
    print(choose_list)

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