day-8 homework

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

student = {
    'name': '张三',
    'age': 28,
    'score': 89,
    'tel': '18273923883'
}
print(student)

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

all_students = [
    {'name': '张三', 'age': 28, 'score': 89, 'tel': '18273923883'},
    {'name': '小明', 'age': 18, 'score': 99, 'tel': '15382933980'},
    {'name': 'Lucy', 'age': 28, 'score': 99, 'tel': '18273923818'},
    {'name': '路飞', 'age': 17, 'score': 78, 'tel': '15382933982'},
    {'name': 'Mike', 'age': 22, 'score': 40, 'tel': '18273923838'},
    {'name': 'Tom', 'age': 16, 'score': 90, 'tel': '15382933981'},
]
print('==============第a,b,c题==============')
count = 0    # 不及格人数
count2 = 0   # 未成年人数

for stu_dict in all_students:
    # a.统计不及格学生的个数
    # b.打印不及格学生的名字和对应的成绩
    if stu_dict['score'] < 60:
        print('%s: %d' % (stu_dict['name'], stu_dict['score']))
        count += 1

    # c.统计未成年学生的个数
    if stu_dict['age'] < 18:
        count2 += 1
# d.打印手机尾号是8的学生的名字
    if stu_dict['tel'][-1] == '8':
        print('%s的手机号:%s' % (stu_dict['name'], stu_dict['tel']))

print('不及格学生人数: %d' % count)
print('未成年学生人数: %d' % count2)

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

print('======================')
max_score = 0
for stu_dict in all_students:
    if stu_dict['score'] > max_score:
        print(stu_dict['score'], type(stu_dict['score']))
        max_score = stu_dict['score']

for stu_dict in all_students:
    if stu_dict['score'] == max_score:
        print(stu_dict['name'], max_score)

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

all_students.sort(key=lambda x: x['age'], reverse=True)
print(all_students)

选择排序

# 1, 5 ,6 ,2,3  --> 1, 2, 3,5,6
"""
nums = [1, 5, 6, 2, 3]
[1, 5, 6, 2, 3]
[1, 2, 6, 5, 3]
[1, 2, 3, 6, 5]  
[1, 2, 3  5, 6]     
"""
nums = [1, 5, 6, 2, 3]
length = len(nums)

for index1 in range(length-1):
    for index2 in range(index1+1, length):
        if nums[index2] < nums[index1]:
            nums[index1], nums[index2] = nums[index2], nums[index1]
print(nums)

length = len(all_students)
for index1 in range(length-1):
    for index2 in range(index1+1, length):
        if all_students[index2]['score'] < all_students[index1]['score']:
            all_students[index1], all_students[index2] = all_students[index2], all_students[index1]
print(all_students)

你可能感兴趣的:(day-8 homework)