day8-字典作业

  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别

student = {
    'name': 'Tom',
    'age': 18,
    'score': 99,
    'tel': 9865665,
    'gender': 'man'
}
  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )

students = [
    {'name': 'Tom', 'age': 18, 'score': 99, 'tel': 9865665, 'gender': 'man'},
    {'name': 'Bob', 'age': 22, 'score': 59, 'tel': 6543453, 'gender': 'woman'},
    {'name': 'Jan', 'age': 16, 'score': 77, 'tel': 1234578, 'gender': 'man'},
    {'name': 'Tony', 'age': 17, 'score': 58, 'tel': 1254877, 'gender': 'woman'},
    {'name': 'Janny', 'age': 19, 'score': 83, 'tel': 1546784},
    {'name': 'Danny', 'age': 21, 'score': 63, 'tel': 4874587, 'gender': 'man'},
    {'name': 'Mary', 'age': 25, 'score': 99, 'tel': 4872568}
]
  1. 统计不及格学生的个数

count = 0
for i in students:
    if i.get('score') < 60:
        count += 1
print(count)
  1. 打印不及格未成年学生的名字和对应的成绩

for i in students:
    if i.get('score') < 60 and i.get('age') < 18:
        print(i.get('name'), i.get('score'))
  1. 求所有男生的平均年龄

total_age = 0
for i in students:
    total_age += i.get('age')
print(total_age/len(students))
  1. 打印手机尾号是8的学生的名字

for i in students:
    if i.get('tel')[-1] == '8':
        print(i.get('name'))
  1. 打印最高分和对应的学生的名字

new_student = {}
max_score = students[0]['score']
for i in students:
    if i.get('score') > max_score:
        new_student.clear()
        new_student.setdefault(i.get('name'), i.get('score'))
    elif i.get('score') == max_score:
        new_student.setdefault(i.get('name'), i.get('score'))
print(new_student)
  1. 删除性别不明的所有学生

for i in students:
    if i.get('gender') is None:
        students.remove(i)
print(students)
  1. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)

new_students = sorted(students, key=lambda x: x['score'], reverse=True)
print(new_students)
  1. 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)

class1 = {
    'name': 'python2301',
    'location': '21教',
    'lecturer': {
        'name': 'yuting',
        'age': 18,
        'gender': '女',
        'tel': '1651336851'
    },
    'headTeacher': {
        'name': '萍姐',
        'age': 18,
        'tel': 1646531684
    },
    'students': [
        {'name': 'stu1', 'age': 18, 'tel': 123, '专业': '语文', 'score': 100, 'linkman': {'name': '张三', 'tel': 59858456}},
        {'name': 'stu2', 'age': 19, 'tel': 133, '专业': '数学', 'score': 99, 'linkman': {'name': '李四', 'tel': 523568325}},
        {'name': 'stu3', 'age': 20, 'tel': 143, '专业': '英语', 'score': 98, 'linkman': {'name': '王五', 'tel': 984658659}},
        {'name': 'stu4', 'age': 16, 'tel': 153, '专业': '物理', 'score': 97, 'linkman': {'name': '任六', 'tel': 364131256}}
    ]
}
  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. 利用列表推导式获取所有狗的品种

['银狐', '法斗', '土狗', '哈士奇', '银狐', '土狗']
breed_list = [i['breed'] for i in dogs]
print(breed_list)
  1. 利用列表推导式获取所有白色狗的名字

['贝贝', '可乐']
name = [i['name'] for i in dogs if i['color'] == '白色']
print(name)
  1. 给dogs中没有性别的狗添加性别为 '公'

for i in dogs:
    i.setdefault('gender', '公')
print(dogs)
  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、付费专栏及课程。

余额充值