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

推荐阅读更多精彩内容

  • 1.编写一个函数,求1+2+3+...+N 2.编写一个函数,求多个数中的最大值 3.编写一一个函数,实现摇色子的...
    02b2972e6a93阅读 167评论 0 3
  • 1.声明一个字典保存一个学生的信息,学生信息中包括: 姓名、年龄、成绩(单科)、电话 2.声明一个列表,在列表中保...
    曹哥_7d59阅读 79评论 0 0
  • 1)这本书为什么值得看: Python语言描述,如果学的Python用这本书学数据结构更合适 2016年出版,内容...
    孙怀阔阅读 12,625评论 0 15
  • 8月22日-----字符串相关 2-3 个性化消息: 将用户的姓名存到一个变量中,并向该用户显示一条消息。显示的消...
    future_d180阅读 1,001评论 0 1
  • 从今天开始 我关闭了我内心世界的大门 陌生人 勿进
    30度以南阅读 215评论 0 0