""" Auther:少校 Time:2025/3/31 16:54 越努力,越幸运 """ # 1. 合并两个字典 dict1={1:1,2:2} dict2={3:3,4:4} for x in dict2: dict1[x] =dict2[x] print(dict1) # 2. 遍历字典的键和值 dict1={'name': '酸奶', 'price': 3, 'count': 18, 'discount': 0.95} print({(x , dict1[x]) for x in dict1}) # 3. 检查键是否存在于字典中 if "张三" not in dict1: print("张三不在dict1") else: print("张三在dict1") # 4. 获取字典所有键组成的列表 print([x for x in dict1]) # 5. 获取字典所有值组成的列表 print([dict1[x] for x in dict1]) # 6. 将两个列表合并为字典 list1 = ['name', 'age', 'score', 'gender'] list2 = ['张三', 19, 99, '男'] # 结果: {'name': '张三', 'age': 19, 'score': 99, 'gender':'男'} dict1={list1[x]:list2[x] for x in range(len(list1))} print(dict1) # 7.字典键值互换 print({dict1[x]:x for x in dict1}) # 8.统计字典中值的数据类型分布 dict1={'name': '酸奶', 'price': 3, 'count': 18, 'discount': 0.95} dict2={} for x in dict1: if type(dict1[x]) not in dict2: dict2[type(dict1[x])]=1 else: dict2[type(dict1[x])]+=1 print(dict2) # 9.统计字符串中各字符出现次数 str1="pythonpppppyythooon" print({x: str1.count(x) for x in str1}) # 10. 已知一个变量保存图书馆所有图书的信息(每一本书包括:书名、作者、类别、是否已经借出、借出时间、借书人)。 # 然后完成下面的题目: books = [ { "书籍名称": "Python编程:从入门到实践", "作者": "Eric Matthes", "类别": "计算机科学", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "流畅的Python", "作者": "Luciano Ramalho", "类别": "计算机科学", "是否已经借出": True, "借出时间": '2025-2-18', "借书人": {'name': '小明', '学号': '100929292'} }, { "书籍名称": "百年孤独", "作者": "加西亚·马尔克斯", "类别": "文学", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "人类简史", "作者": "尤瓦尔·赫拉利", "类别": "历史", "是否已经借出": True, "借出时间": '2024-6-5', "借书人": {'name': '小花', '学号': '9283839393'} }, { "书籍名称": "白夜行", "作者": "东野圭吾", "类别": "小说", "是否已经借出": True, "借出时间": '2024-3-20', "借书人": {'name': '张三', '学号': '2389092221'} }, { "书籍名称": "三体", "作者": "刘慈欣", "类别": "文学", "是否已经借出": True, "借出时间": '2024-12-5', "借书人": {'name': '李四', '学号': '00009282833'} }, { "书籍名称": "深度学习入门", "作者": "斋藤康毅", "类别": "人工智能", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "追风筝的人", "作者": "卡勒德·胡赛尼", "类别": "文学", "是否已经借出": True, "借出时间": '2025-3-18', "借书人": {'name': '何丹', '学号': '01299923'} }, { "书籍名称": "算法图解", "作者": "Aditya Bhargava", "类别": "计算机科学", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "活着", "作者": "余华", "类别": "文学", "是否已经借出": True, "借出时间": '2025-3-20', "借书人": {'name': '赵桂珍', '学号': '01029222'} }, { "书籍名称": "时间简史", "作者": "史蒂芬·霍金", "类别": "科普", "是否已经借出": True, "借出时间": '2024-11-22', "借书人": {'name': '刘小龙', '学号': '10209292'} }, { "书籍名称": "围城", "作者": "钱钟书", "类别": "文学", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "Effective Python", "作者": "Brett Slatkin", "类别": "计算机科学", "是否已经借出": True, "借出时间": '2025-2-28', "借书人": {'name': '赵桂珍', '学号': '01029222'} }, { "书籍名称": "挪威的森林", "作者": "村上春树", "类别": "文学", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "数据结构与算法分析", "作者": "Mark Allen Weiss", "类别": "计算机科学", "是否已经借出": True, "借出时间": '2024-6-5', "借书人": {'name': '小花', '学号': '9283839393'} }, { "书籍名称": "机器学习实战", "作者": "Peter Harrington", "类别": "人工智能", "是否已经借出": False, "借出时间": None, "借书人": None }, { "书籍名称": "平凡的世界", "作者": "路遥", "类别": "小说", "是否已经借出": True, "借出时间": '2025-3-15', "借书人": {'name': '小花', '学号': '9283839393'} } ] # 1)打印所有的书名 print([x["书籍名称"] for x in books]) # 2)获取所有"计算机"类别的书名 print([x["书籍名称"] for x in books if x["类别"]=="计算机科学"]) # 3)统计不同类别的书籍的数量 dict1={} # for x in books: # if x["类别"] not in dict1: # dict1[x["类别"]] = 1 # else: # dict1[x["类别"]] += 1 # print(dict1) for x in books: t = x["类别"] dict1[t] = dict1.get(t,0) +1 print(dict1) # 4) 获取借书的数量超过一本的学生的信息 # jie={} # for x in books: # if x["借书人"] != None: # if x["借书人"]['name'] not in jie: # jie[x["借书人"]['name']] =1 # else: # jie[x["借书人"]['name']] += 1 # print(jie) # dict2=[] # for x in jie: # if jie[x] >1 : # for y in books: # if y["借书人"] != None: # if y["借书人"]['name']== x and y["借书人"] not in dict2: # dict2.append(y["借书人"]) # print(dict2) jie=[] for x in books: if x["借书人"] is not None: jie.append(x["借书人"]) jie2=[] for x in jie: if jie.count(x) > 1 and x not in jie2: jie2.append(x) print(jie2) # 5)统计已经超时的图书的名字和对应的借书人学号(借出一个月没有还就算超时) -- 涉及python时间操作,可以不做 from datetime import datetime for x in books: if x["借出时间"] is not None and (datetime.now() - datetime.strptime(x["借出时间"], "%Y-%m-%d")).days > 30: print(x["书籍名称"],x["借书人"]['学号'])
05.homework
本节6168字2025-04-01 10:48:45