当前位置:网站首页>day8字典作业
day8字典作业
2022-08-05 05:13:00 【非鱼丶丶】
- 定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
student_message = {
'name': 'A', 'age': 18, 'English_grades': 59, 'tel': '123456789', 'gender': 'man'}
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students = [{ 'name': 'A', 'age': 19, 'grades': 59, 'tel': '123456789', 'gender': 'man'}, { 'name': 'B', 'age': 18, 'grades': 66, 'tel': '123456789', 'gender': 'woman'}, { 'name': 'C', 'age': 18, 'grades': 95, 'tel': '123456789', 'gender': 'man'}, { 'name': 'D', 'age': 17, 'grades': 88, 'tel': '123456788', 'gender': 'woman'}, { 'name': 'E', 'age': 18, 'grades': 32, 'tel': '123456789', 'gender': 'man'}, { 'name': 'F', 'age': 18, 'grades': 76, 'tel': '123456788', 'gender': 'unknow'} ]- 统计不及格学生的个数
count = 0 for x in students: if x.get('grades') < 60: count += 1 print(count)- 打印不及格未成年学生的名字和对应的成绩
for x in students: if x.get('grades') < 60: print(x.get('name'), x.get('grades'))- 求所有男生的平均年龄
sum1 = count = 0 for x in students: if x.get('gender') == 'man': count += 1 sum1 += x.get('age') print(sum1 / count)- 打印手机尾号是8的学生的名字
for x in students: if x.get('tel')[-1] == '8': print(x.get('name'))- 打印最高分和对应的学生的名字
a = 0 for x in students: if x.get('grades') > a: a = x.get('grades') b = x.get('name') print(a, b)- 删除性别不明的所有学生
for x in students[:]: if x['gender'] == 'unknow': students.remove(x) print(students)- 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
new_student = [] list1 = [] dict1 = { } for x in range(len(students)): list1.append(students[x].get('grades')) dict1[students[x]['grades']] = x list1.sort(reverse=True) for i in list1: new_student.append(students[dict1[i]]) print(new_student)定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = {
'class_name': 'Python2204',
'address': '15教',
'lecturer': {
'name': '余婷', 'age': 18, 'qq': '726550822', 'gender': '女'},
'class_teacher': {
'name': '静静', 'tel': '110'},
'students': [
{
'name': 'stu1', 'age': 21, 'major': '会计', 'tel': '120', 'contacts': {
'name': '张三', 'tel': '162723'}},
{
'name': 'stu2', 'age': 30, 'major': '电子', 'tel': '219223', 'contacts': {
'name': '小明', 'tel': '281912'}},
{
'name': 'stu3', 'age': 19, 'major': '旅游管理', 'tel': '123233', 'contacts': {
'name': '小花', 'tel': '886552'}},
{
'name': 'stu4', 'age': 25, 'major': '通信', 'tel': '4444221', 'contacts': {
'name': '李四', 'tel': '22342345'}},
{
'name': 'stu5', 'age': 25, 'major': '机械', 'tel': '223111', 'contacts': {
'name': '王五', 'tel': '555632'}},
{
'name': 'stu6', 'age': 23, 'major': '数学', 'tel': '234234', 'contacts': {
'name': '赵六', 'tel': '96533'}}
]
}
已知一个列表保存了多个狗对应的字典:
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': '母'} ]利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
print([x['breed'] for x in dogs])利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
print([x['name'] for x in dogs if x['color'] == '白色'])- 给dogs中没有性别的狗添加性别为 ‘公’
[x.setdefault('gender', '公') for x in dogs] print(dogs)统计 ‘银狐’ 的数量
count = 0 for x in dogs: if x['breed'] == '银狐': count += 1 print(count)
边栏推荐
猜你喜欢

类的底层机制

位运算符与逻辑运算符的区别

结构光三维重建(一)条纹结构光三维重建

DOM and its applications

【过一下8】全连接神经网络 视频 笔记

开发一套高容错分布式系统

Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
![[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)](/img/86/9c9a2541f2b7089ae47e9832fffdb3.png)
[Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)

jvm 三 之堆与栈
![[Go through 7] Notes from the first section of the fully connected neural network video](/img/e2/1107171b52fe9dcbf454f7edcdff77.png)
[Go through 7] Notes from the first section of the fully connected neural network video
随机推荐
Using QR codes to solve fixed asset management challenges
【过一下6】机器视觉视频 【过一下2被挤掉了】
day12函数进阶作业
vscode+pytorch使用经验记录(个人记录+不定时更新)
物理层的接口有哪几个方面的特性?各包含些什么内容?
学习总结week3_2函数进阶
Community Sharing|Tencent Overseas Games builds game security operation capabilities based on JumpServer
Excel Paint
Analysis of Mvi Architecture
Structured Light 3D Reconstruction (2) Line Structured Light 3D Reconstruction
coppercam入门手册[6]
Judgment statement _switch and case
OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
有用番茄来监督自己的同道中人吗?加一下我的自习室,一起加油
redis复制机制
span标签和p标签的区别
SQL(一) —— 增删改查
Detailed Explanation of Redis Sentinel Mode Configuration File
Geek卸载工具
UVA10827