当前位置:网站首页>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)
边栏推荐
- 使用二维码解决固定资产管理的难题
- Difference between for..in and for..of
- 【过一下8】全连接神经网络 视频 笔记
- Qt制作18帧丘比特表白意中人、是你的丘比特嘛!!!
- Returned object not currently part of this pool
- OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
- [Student Graduation Project] Design and Implementation of the Website Based on the Web Student Information Management System (13 pages)
- 重新审视分布式系统:永远不会有完美的一致性方案……
- 【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
- BFC(Block Formatting Context)
猜你喜欢
span标签和p标签的区别
Qt produces 18 frames of Cupid to express his love, is it your Cupid!!!
"Recursion" recursion concept and typical examples
Flex layout frog game clearance strategy
Wise Force Deleter强制删除工具
RL reinforcement learning summary (1)
How can Flutter parent and child components receive click events
【过一下12】整整一星期没记录
LeetCode: 1403. Minimum subsequence in non-increasing order [greedy]
【过一下8】全连接神经网络 视频 笔记
随机推荐
【练一下1】糖尿病遗传风险检测挑战赛 【讯飞开放平台】
第四讲 反向传播随笔
多线程查询结果,添加List集合
vscode+pytorch使用经验记录(个人记录+不定时更新)
"Recursion" recursion concept and typical examples
uboot enable debug printing information
redis复制机制
【记一下1】2022年6月29日 哥和弟 双重痛苦
redis 缓存清除策略
Lecture 2 Linear Model Linear Model
Reverse theory knowledge 4
Convert the paper official seal in the form of a photo into an electronic official seal (no need to download ps)
OFDM Lecture 16 5 -Discrete Convolution, ISI and ICI on DMT/OFDM Systems
【过一下8】全连接神经网络 视频 笔记
After controlling the export file in MySQL, it becomes \N. Is there any solution?
Excel Paint
server disk array
jvm 三 之堆与栈
Requests库部署与常用函数讲解
Difference between for..in and for..of