2022年4月20号第二周第三天作业

  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student = {'name': '罗浩', 'age': 20, 'score': 100, 'tel': '120', 'gender': '男'}
  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

    1. 统计不及格学生的个数
    2. 打印不及格未成年学生的名字和对应的成绩
    3. 求所有男生的平均年龄
    4. 打印手机尾号是8的学生的名字
    5. 打印最高分和对应的学生的名字
    6. 删除性别不明的所有学生
    7. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students = [
        {'name': '罗浩', 'score': 59, 'age': 22, 'tel': '254284', 'gender': '不明'},
        {'name': '陈佳良', 'score':85 ,'age': 32, 'tel': '62731246', 'gender': '男'},
        {'name': '蒋天仁', 'score': 99, 'age': 22, 'tel': '1292831', 'gender': '男'},
        {'name': '赵欣雅', 'score': 98, 'age': 18, 'tel': '72289392', 'gender': '女'},
        {'name': '冉昭红', 'score': 84, 'age': 18, 'tel': '2737288', 'gender': '女'},
        {'name': '郭钊', 'score': 55, 'age': 24, 'tel': '27361028', 'gender': '男'}
]
# 1.
count = 0
for i in students:
    if i['score'] < 60:
        count += 1
print('不及格学生的个数:', count)
# 2.
for i in students:
    if i['score'] < 60:
        print('不及格未成年学生的名字和对应的成绩: ', i['name'], i['score'])
total = 0
malestu = 0
# 3.
for i in students:
    if i['gender'] == '男':
    	total += i['age']
        malestu += 1
print('所有男生的平均年龄: ', total//malestu)
# 4.
for i in students:
    if i['tel'][-1] == '8':
        print('手机尾号是8的学生的名字: ', i['name'])
# 5.
max1 = 0
maxName = ''
for i in students:
    if i['score'] > max1:
        max1 = i['score']
        maxName = i['name']
print('最高分和对应的学生的名字: ', max1, maxName)
# 6.
for i in range(-len(students), 0, 1):
    if students[i]['gender'] == '不明':
        students.remove(students[i])
print(students)
# 7.
scores = [i['score'] for i in students]
for i in range(0, len(scores)):
    for j in range(1, len(scores)):
        if scores[i] > scores[j]:
            students[i], students[j] = students[j], students[i]
print(students)
  1. 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = {
    'name': 'Python2202',
    'address': '14教室',
    'lecturer': {'name': '余婷', 'qq': '726550822', 'gender': '女', 'age': 18},
    'head_teacher': {'name': '舒玲', 'tel': '110', 'gender': '女', 'age': 18},
    'students': [
        {'name': '罗浩', 'tel': '120', 'gender': '男', 'major': '软件工程', 'age': 20, 'linkman': {'name': '爸爸', 'tel': '29372'}},
        {'name': '陈佳良', 'tel': '62736', 'gender': '女', 'major': '电子信息', 'age': 23, 'linkman': {'name': '妈妈', 'tel': '78221'}},
        {'name': '蒋天仁', 'tel': '1292831', 'gender': '女', 'major': '会计', 'age': 27, 'linkman': {'name': '姐姐', 'tel': '28239'}},
        {'name': '赵欣雅', 'tel': '72289392', 'gender': '男', 'major': '数学', 'age': 21, 'linkman': {'name': '哥哥', 'tel': '29102'}},
        {'name': '冉昭红', 'tel': '2737288', 'gender': '女', 'major': '计算机', 'age': 32, 'linkman': {'name': '姐夫', 'tel': '236721'}},
        {'name': '郭钊', 'tel': '27361028', 'gender': '男', 'major': '物理', 'age': 19, 'linkman': {'name': '奶奶', 'tel': '34293'}},
        {'name': '付琴', 'tel': '829129', 'gender': '女', 'major': '石油', 'age': 18, 'linkman': {'name': '爷爷', 'tel': '229311'}}
    ]
}
  1. 已知一个列表保存了多个狗对应的字典:

    dogs = [
      {'name': '贝贝', 'color': '白色', 'breed': '银狐', 'age': 3, 'gender': '母'},
      {'name': '花花', 'color': '灰色', 'breed': '法斗', 'age': 2},
      {'name': '财财', 'color': '黑色', 'breed': '土狗', 'age': 5, 'gender': '公'},
      {'name': '包子', 'color': '黄色', 'breed': '哈士奇', 'age': 1},
      {'name': '可乐', 'color': '白色', 'breed': '银狐', 'age': 2},
      {'name': '旺财', 'color': '黄色', 'breed': '土狗', 'age': 2, 'gender': '母'}
    ]
    
    1. 利用列表推导式获取所有狗的品种

      [‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]

    breeds = [i['breed'] for i in dogs]
    print(breeds)
    
    1. 利用列表推导式获取所有白色狗的名字

      [‘贝贝’, ‘可乐’]

    names = [i['name'] for i in dogs if i['color'] == '白色']
    print(names)
    
    1. 给dogs中没有性别的狗添加性别为 ‘公’
    for i in dogs:
        i.setdefault('gender', '公')
    print(dogs)
    
    1. 统计 ‘银狐’ 的数量
# 方法1
count = 0
for i in dogs:
    if i['breed'] == '银狐':
        count += 1
print(count)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值