Day 8 Dictionary 基础操作
字典
-
字典用来保存多个意义不相同的数据,用键对值进行说明
-
列表保存多个意义相同的数据
-
字典是容器型数据类型.{}作为容器标志,多个键值对直接按用逗号隔开,键与值之间用冒号
-
字典是可变的,支持增删改操作
-
字典是无序的
print({"a":10, "b":20} == {"b":20, "a":10}) #True,只要元素相同,就是同一个字典
-
字典的元素:由键和值组成,其中值才是真正保存的数据,键是用来对值进行说明的
-
字典的键: 键只能是不可变的数据类型(数字,字符串,布尔值都是不可变的数据类型),字典的键是唯一的
#键是唯一的,,,只保存最后一次遇到的键值对 dict3 = {"a":10,"b":20,"a":30} print(dict3) #空字典 dict1 = {}
-
元组是不可变的列表,列表是可变的容器型数据类型
-
字典的值: 没有任何要求(任何类型数据都可以作为字典的值)
查
-
字典[键] - 获取字典中指定键对应的值,键不存在会报错
dict_dog = {"name": "旺财", "age": 3, "color": "黄色", "breed": "土狗"} print(dict_dog["name"]) print(dict_dog["color"])
-
字典.get(键) - 获取指定键对应的值,键不存在不会报错,返回空值None
dict_dog = {"name": "旺财", "age": 3, "color": "黄色", "breed": "土狗"} print(dict_dog.get("color")) print(dict_dog.get("weight")) #None
-
字典.get(键,默认值) - 获取指定键对应的值,键不存在,返回指定的默认值
dict_dog = {"name": "旺财", "age": 3, "color": "黄色", "breed": "土狗"} print(dict_dog.get("name", "无名氏")) #旺财 print(dict_dog.get("weight", "无名氏")) #无名式 print(dict_dog.get("breed", "土狗")) #土狗
增
-
字典[键] = 值 - 键不存在就在字典中添加新的键值对
dog = {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗'} print(dog) # {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗'} dog['weight'] = 20 print(dog) # {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗', 'weight': 20}
-
字典.setdefault(键,值) - 如果键值对不存在,就在字典中添加键值对,如果存在,就修改键的值
dog.setdefault('gender', '母狗') print(dog) # {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗', 'weight': 20, 'gender': '母狗'} dog.setdefault('age', 4) print(dog) # {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗', 'weight': 20, 'gender': '母狗'}
改
-
字典[键] = 值 - 修改键对应的值
dog = {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗'} print(dog) dog['name'] = '来钱儿' print(dog)
删
-
del 字典[键] - 删除指定键对应的键值对
dog = {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗'} print(dog) del dog['color'] print(dog) # {'name': '财财', 'age': 3, 'breed': '土狗'}
-
字典.pop(键) - 取走字典中指定键对应的值,并且返回
dog = {'name': '财财', 'age': 3, 'color': '黄色', 'breed': '土狗'} print(dog) result = dog.pop('name') print(dog, result) # {'age': 3, 'color': '黄色', 'breed': '土狗'} 财财
-
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
Student = {"name": "张林子", "age":18, "scores":100, "tel":19130353125,"gender":"男"}
-
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
-
统计不及格学生的个数
-
打印不及格未成年学生的名字和对应的成绩
-
求所有男生的平均年龄
-
打印手机尾号是8的学生的名字
-
打印最高分和对应的学生的名字
-
删除性别不明的所有学生
-
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
# 0.定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别 Students = [ {"name": "张林子", "age": 18, "scores": 98, "tel": "19130353125", "gender": "男"}, {"name": "张子林", "age": 14, "scores": 59, "tel": "19130353125", "gender": "不明"}, {"name": "林子张", "age": 18, "scores": 58, "tel": "19130353128", "gender": "男"}, {"name": "林张子", "age": 15, "scores": 99, "tel": "19130353125", "gender": "女"}, {"name": "子林张", "age": 22, "scores": 89, "tel": "19130353128", "gender": "男"}, {"name": "子张林", "age": 26, "scores": 56, "tel": "19130353125", "gender": "女"} ] # 1. 统计不及格学生的个数 count = 0 for i in Students: if i["scores"] < 60: count += 1 print("不及格学生人数:", count, ) # 2打印不及格未成年学生的名字和对应的成绩 print([i["name"] for i in Students if i["age"] < 18 and i["scores"] < 60], [i["scores"] for i in Students if i["age"] < 18 and i["scores"] < 60]) #3 求所有男生的平均年龄 sum_age = 0 count = 0 for i in Students: sum_age += i["age"] count += 1 print("平均年龄:{:.2f}".format(sum_age / count)) # 4打印手机尾号是8的学生的名字 print([i["tel"] for i in Students if i["tel"][-1] == 8]) #5 打印最高分和对应的学生的名字 max_scores = Students[0]["scores"] name = Students[0]["name"] for i in Students: if max_scores < i["scores"] : max_scores = i["scores"] name = i["name"] print(name,max_scores) #6 删除性别不明的所有学生 Students2 = [] for i in Students: if i["gender"] !="不明": Students2.append(i) print(Students2) #7 将列表按学生成绩从大到小排序(挣扎一下,不行就放弃 new_student=[] while True: max_score = Students[0]["scores"] id = 0 for i in range(len(Students)): if Students[i]["scores"] >= max_score: max_score = Students[i]["scores"] id = i s = Students.pop(id) new_student.append(s) if Students == []: break print(new_student)
-
-
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = { "name": "python2301", "location": "21教", "lecturer": { "name": "玉婷", "age": 18, "gender": "nv", "tel": "13678182302", }, "head_teacher": { "name": "萍姐", "age": 18, "tel": "110" }, "all_student": [ { "name": "王八", "age": 18, "tel": "119", "专业": "计算机", "score": 100, "linkman": {"name", "张三", "tel", "1828272775"} }, { "name": "初七", "age": 18, "tel": "119", "专业": "计算机", "score": 100, "linkman": {"name", "张四", "tel", "1828272775"} }, { "name": "胡五", "age": 18, "tel": "119", "专业": "计算机", "score": 100, "linkman": {"name", "张五", "tel", "1828272775"} }, { "name": "李四", "age": 18, "tel": "119", "专业": "计算机", "score": 100, "linkman": {"name", "张六", "tel", "1828272775"} }, { "name": "张三", "age": 18, "tel": "119", "专业": "计算机", "score": 100, "linkman": {"name", "张六", "tel", "1828272775"} } ] }
-
已知一个列表保存了多个狗对应的字典:
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([i["breed"] for i in dogs])
-
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
print([i["name"] for i in dogs if i["color"] == "白色"])
-
给dogs中没有性别的狗添加性别为 ‘公’
for i in dogs: i.setdefault("gender","公") print(dogs)
-
统计 ‘银狐’ 的数量
count = 0 for i in dogs: if i["breed"] == "银狐": count += 1 print(count)
-
-