- 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student = {'name': '奈德丽', 'age': 18, '语文': 70, 'tel': '15760078242', 'gender': '男'}
- 定义一个列表,在列表中保存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': '男'}
]
- 统计不及格学生的个数
count = 0
for student in students:
if student['score'] < 60:
count += 1
print('不及格学生的个数有:', count, sep='')
- 打印不及格未成年学生的名字和对应的成绩
for student in students:
if student['age'] < 18 and student['score'] < 60:
print(student['name'], student['score'])
- 求所有男生的平均年龄
count = sum1 = 0
for student in students:
if student['gender'] == '男':
count += 1
sum1 += student['score']
print(sum1 / count)
- 打印手机尾号是8的学生的名字
for student in students:
if student['tel'][-1] == '8':
print(student['name'], end=' ')
- 打印最高分和对应的学生的名字
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
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)
- 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
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)
- 定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
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': '男'}
]
}
]
- 已知一个列表保存了多个狗对应的字典:
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': '母'}
]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
result = [x['breed'] for x in dogs]
print(result)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
result = [x['name'] for x in dogs if x['color'] == '白色']
print(result)
- 给dogs中没有性别的狗添加性别为 ‘公’
#方法1
for dog in dogs:
dog.setdefault('gender', '公')
print(dogs)
#方法2
[dog.setdefault('gender', '公') for dog in dogs]
print(dogs)
- 统计 ‘银狐’ 的数量
count = 0
for dog in dogs:
if dog['breed'] == '银狐':
count += 1
print(count)