-
定义一个变量保存一个学生的信息,学生信息中包括:姓名、年龄、成绩(单科)、电话、性别
student = {'name': 'stu0', 'age': 25, 'score': 95, 'tel': '9876543210', 'gender': '男'} print(student)
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [ {'name': 'stu1', 'age': 22, 'score': 95, 'tel': '25428456', 'gender': '男'}, {'name': 'stu2', 'age': 32, 'score': 58, 'tel': '62731246', 'gender': '女'}, {'name': 'stu3', 'age': 22, 'score': 65, 'tel': '12928317', 'gender': '男'}, {'name': 'stu4', 'age': 18, 'score': 78, 'tel': '72289392', 'gender': '不明'}, {'name': 'stu5', 'age': 16, 'score': 72, 'tel': '27372885', 'gender': '女'}, {'name': 'stu6', 'age': 15, 'score': 45, 'tel': '27361028', 'gender': '男'}, {'name': 'stu7', 'age': 40, 'score': 95, 'tel': '27565428', 'gender': '不明'}, ]
-
统计不及格学生的个数
# 方法1: count = 0 for stu in students: if stu['score'] < 60: count += 1 print(count) # 方法2: np_students = [stu['name'] for stu in students if stu['score'] < 60] print(len(np_students))
-
打印不及格未成年学生的名字和对应的成绩
# 方法1: for stu in students: if stu['score'] < 60 and stu['age'] < 18: print(stu['name'], stu['score']) # 方法2: np_ua_students = {stu['name']: stu['score'] for stu in students if stu['age'] < 18 and stu['score'] < 60} print(np_ua_students)
-
求所有男生的平均年龄
# 方法1: sum_age = 0 count = 0 for stu in students: if stu['gender'] == '男': sum_age += stu['age'] count += 1 print(round(sum_age / count, 2)) # 方法2: boy_ages = [stu['age'] for stu in students if stu['gender'] == '男'] print(round(sum(boy_ages) / len(boy_ages), 2))
-
打印手机尾号是8的学生的名字
# 方法1: for stu in students: if stu['tel'][-1] == '8': print(stu['name'], end='\t') print() # 方法2: ei_students = [stu['name'] for stu in students if stu['tel'][-1] == '8'] print(ei_students)
-
打印最高分和对应的学生的名字
# 方法1: max_score = students[0]['score'] max_s_name = [] for stu in students: if stu['score'] > max_score: max_s_name.clear() max_s_name.append(stu['name']) max_score = stu['score'] elif stu['score'] == max_score: max_s_name.append(stu['name']) print(max_s_name, max_score) # 方法2: max_score = max([stu['score'] for stu in students]) max_score_stu = [stu['name'] for stu in students if stu['score'] == max_score] print(max_score_stu, max_score)
-
删除性别不明的所有学生
new_students = [stu for stu in students if stu['gender'] != '不明'] print(new_students)
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 方法1: for i in range(1, len(students)): for j in range(len(students) - i): if students[j]['score'] < students[j + 1]['score']: students[j], students[j + 1] = students[j + 1], students[j] print(students) # 方法2: scores = [stu['score'] for stu in students] while len(scores) > 1: students.append(students.pop(scores.index(max(scores)))) del scores[scores.index(max(scores))] students.append(students.pop(0)) print(students)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class_msg = { 'class_name': 'python', 'address': '404教室', 'head_teacher': 'sl', 'lecturer': 'tj', 'students':[ {'name': 'stu1', 'gender': 1, 'age': 25, 'tel': '9876543210', 'linkman': {'name': 'link1', 'tel': '1111111111'}}, {'name': 'stu2', 'gender': 0, 'age': 18, 'tel': '8765432109', 'linkman': {'name': 'link1', 'tel': '0000000000'}}, {'name': 'stu3', 'gender': 0, 'age': 22, 'tel': '7654321098', 'linkman': {'name': 'link1', 'tel': '2222222222'}}, {'name': 'stu4', 'gender': 1, 'age': 30, 'tel': '6543210987', 'linkman': {'name': 'link1', 'tel': '3333333333'}}, {'name': 'stu5', 'gender': 1, 'age': 22, 'tel': '5432109876', 'linkman': {'name': 'link1', 'tel': '4444444444'}}, {'name': 'stu6', 'gender': None, 'age': 36, 'tel': '4321098765', 'linkman': {'name': 'link1', 'tel': '5555555555'}}, {'name': 'stu7', 'gender': 1, 'age': 16, 'tel': '3210987654', 'linkman': {'name': 'link1', 'tel': '6666666666'}}, {'name': 'stu8', 'gender': 0, 'age': 20, 'tel': '2109876543', 'linkman': {'name': 'link1', 'tel': '7777777777'}}, {'name': 'stu9', 'gender': 1, 'age': 24, 'tel': '1098765432', 'linkman': {'name': 'link1', 'tel': '8888888888'}}, {'name': 'stu10', 'gender': 0, 'age': 19, 'tel': '0987654321', 'linkman': {'name': 'link1', 'tel': '9999999999'}}, ] }
-
已知一个列表保存了多个狗对应的字典:
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': '母'} ]
-
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
breeds = [dog['breed'] for dog in dogs] print(breeds)
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
white_dogs = [dog['name'] for dog in dogs if dog['color'] == '白色'] print(white_dogs)
-
给dogs中没有性别的狗添加性别为 ‘公’
for dog in dogs: dog.setdefault('gender', '公') print(dogs)
-
统计 ‘银狐’ 的数量
# 方法1: count = 0 for dog in dogs: if dog['breed'] == '银狐': count += 1 print(count) # 方法2: sf_dogs = [dog['name'] for dog in dogs if dog['breed'] == '银狐'] print(len(sf_dogs))
-
day7-字典作业
最新推荐文章于 2023-09-09 10:54:35 发布