day7-字典作业(1)

  1. 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student = {'name': '奈德丽', 'age': 18, '语文': 70, 'tel': '15760078242', 'gender': '男'}
  1. 定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students =[
    {'name': '奈德丽', 'age': 18, '语文': 70, 'tel': '15760078242', 'gender': '女'},
    {'name': '卡特', 'age': 15, '语文': 55, 'tel': '18818264152', 'gender': '女'},
    {'name': '劫', 'age': 20, '语文': 95, 'tel': '15252852242', 'gender': '不明'},
    {'name': '艾克', 'age': 17, '语文': 78, 'tel': '15961717478', 'gender': '男'},
    {'name': '亚索', 'age': 21, '语文': 40, 'tel': '15162078244', 'gender': '男'},
    {'name': '李青', 'age': 22, '语文': 90, 'tel': '15700481558', 'gender': '男'}
]
  1. 统计不及格学生的个数
count = 0
for student in students:
    if student['score'] < 60:
        count += 1
print('不及格学生的个数有:', count, sep='')
  1. 打印不及格未成年学生的名字和对应的成绩
for student in students:
    if student['age'] < 18 and student['score'] < 60:
        print(student['name'], student['score'])
  1. 求所有男生的平均年龄
count = sum1 = 0
for student in students:
    if student['gender'] == '男':
        count += 1
        sum1 += student['score']
print(sum1 / count)
  1. 打印手机尾号是8的学生的名字
for student in students:
    if student['tel'][-1] == '8':
        print(student['name'], end=' ')
  1. 打印最高分和对应的学生的名字
max_score = students[0]['score']
for student in students[1:]:
    if student['score'] > max_score:
        max_score = student['score']
for student in students:
	if student['score'] == max_score:
	print(max_score,student['name'])
  1. 删除性别不明的所有学生
#方法1
for index in range(len(students)):
    if students[index]['gender'] == '不明':
        del students[index]
        break
print(students)
#方法2
result = [student for student in students if student['score'] != '不明']
print(result)
  1. 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
scores1 = []
scores2 = []
for student in students:
    scores1.append(student['score'])
scores1.sort(reverse=True)
for score in scores1:
    for student in students:
        if student['score'] == score:
            scores2.append(student)
            break
print(scores2)
  1. 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = [
    {'班级名称': 'python-2106', '教室位置': '18教'},
    {
        '班主任信息': [{
            'name': '朱老师', 'age': 18
        }],
        '讲师信息': [{
            'name': '余老师', 'age': 18, 'height': 155, 'weight': 51
        }],
        '学生信息':[
            {'name': '小李', 'age': 18, 'score': 70, 'tel': '15760078242', 'gender': '女'},
            {'name': '小王', 'age': 15, 'score': 55, 'tel': '18818264152', 'gender': '女'},
            {'name': '小罗', 'age': 22, 'score': 90, 'tel': '15700481558', 'gender': '男'}
        ]
    }
]
  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. 利用列表推导式获取所有狗的品种

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

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

    [‘贝贝’, ‘可乐’]

result = [x['name'] for x in dogs if x['color'] == '白色']
print(result)
  1. 给dogs中没有性别的狗添加性别为 ‘公’
#方法1
for dog in dogs:
    dog.setdefault('gender', '公')
print(dogs)
#方法2
[dog.setdefault('gender', '公') for dog in dogs]
print(dogs)
  1. 统计 ‘银狐’ 的数量
count = 0
for dog in dogs:
    if dog['breed'] == '银狐':
        count += 1
print(count)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值