用列表保存不同含义的数据时,不能分辨具体数据的意义,不利于后续数据的操作
stu={'name':'小明','age':45,'height':175,'体重':75,'score':30,}
用一个容器同时保存多个数据的时候的建议:如果多个数据的意义相同用列表,如果多个数据的意义不同用字典
1)字典是容器型数据类型:将{}作为容器的标志,里面多个元素用逗号隔开,元素必须是键值对
{键1:值1,键2:值2,键3:值3,…}
2)字典是可变的(支持增删改);
3)字典是无序的(不支持下标操作)
a.键:键必须是不可变的数据类型的数据;键是唯一的
b.值:值是没有要求的
dictl={} #空字典
print(len(dict1),type(dict1))
#字典是无序的
print({'a':10,'b':20}=={'b':20,'a':10}) #True
1查单个数据(重要) - 获取单个值
字典查找方法 | 语法1 | 语法2 |
---|---|---|
写法 | 字典[键] | 字典.get(键)或字典.get(键,默认值) |
获取指定键对应的值 | 获取指定键对应的值 | |
键如果不存在 | 会报错 | 不会报错,返回空值,或者写的默认值 |
dog={'name':'财财','gender':'母狗','breed':'哈士奇','age':3}
print(dog['gender']) #'母狗'
print(dog.get('age')) #3
print(dog.get('ss',0)) #0
print(dog.get('ss')) #None
print(dog['ss']) #报错
练习:创建一个字典保存一个班级的信息
class1 = {
'name': 'Python2202',
'address': '14教室',
'lecturer': {'name': '余婷', 'qq': '726550822', 'gender': '女', 'age': 18},
'head_teacher': {'name': '舒玲', 'tel': '110', 'gender': '女', 'age': 18},
'students': [
{'name': '罗浩', 'tel': '120', 'gender': '男', 'major': '软件工程', 'age': 20, 'linkman': {'name': 'p1', 'tel': '29372'}},
{'name': 'stu1', 'tel': '62736', 'gender': '女', 'major': '电子信息', 'age': 23, 'linkman': {'name': 'p2', 'tel': '78221'}},
{'name': 'stu2', 'tel': '1292831', 'gender': '女', 'major': '会计', 'age': 27, 'linkman': {'name': 'p3', 'tel': '28239'}},
{'name': 'stu3', 'tel': '72289392', 'gender': '男', 'major': '数学', 'age': 21, 'linkman': {'name': 'p4', 'tel': '29102'}},
{'name': 'stu4', 'tel': '2737288', 'gender': '女', 'major': '计算机', 'age': 32, 'linkman': {'name': 'p5', 'tel': '236721'}},
{'name': 'stu5', 'tel': '27361028', 'gender': '男', 'major': '物理', 'age': 19, 'linkman': {'name': 'p6', 'tel': '34293'}},
{'name': 'stu6', 'tel': '829129', 'gender': '男', 'major': '石油', 'age': 22, 'linkman': {'name': 'p7', 'tel': '229311'}}
]
}
#获取所有学生的名字
for 变量 in 字典:
循环体
注意:字典遍历变量依次取的值是字典的键
增、改
语法1:字典[键]=值 - 如果键不存在就添加键值对,如果键存在就修改指定键对应的值
goods={'name':'老坛酸菜','price':3.5,'weight':300}
print(goods)
goods['shelf_life']=30
print(goods)
goods['price']=3
print(goods)
goods.setdefault('count',100)
print(goods)
goods.setdefault('price',2.5)
print(goods)
语法2:字典.setdefault(键,值) - 添加键值对(键不存在就添加,键存在不会修改)
2.删
1)del 字典[键] - 删除指定键对应的键值对
2)字典.pop(键) - 取出指定键对应的值(键值成对出现)x=字典.pop(键)
相关操作:字典不支持+、*、比较大小
字典支持相等、in、not in
字典的in、not in 判断的是是否存在指定的键
print(10 in {'a':10,'b':20,'c':30}) #False
print('a' in {'a':10,'b':20,'c':30}) #True
dict1={'a':10,'b':20,'c':30}
print(list(dict1)) #['a','b','c']
print(dict('ab','cd','ef')) #{'a':'b','c':'d','e':'f'}
补充:字典转换成列表的时候,是将字典的键作为列表的元素
dict(数据) - 将指定数据转换成字典
对数据的要求:a 数据本身是一个序列
b 序列中的元素必须是有两个元素的小序列
c 小序列中的第一个元素是不可变的类型的数据
1)字典.get(键)
2)字典.clear() - 清空字典
字典.copy() - 拷贝原字典产生一个一模一样的新字典
3)字典.keys() - 获取所有的键(是序列但是不是列表)
字典.values() - 获取所有的值(是序列但是不是列表)
字典.items() - 以元组的形式返回键值对的序列(是序列但是不是列表)
4)字典.update(字典2) - 将字典2所有的键值对都添加到字典1
for key,value in dog.item():
print(key,value) #分别获取键和值
dict1={'name':'张三','age':19}
dict2={'a':10,'b':20,'c':30}
dict1.update(dict2)
print(dict1)
结构1:{表达式1:表达式2 for 变量 in 序列}
结构2:{表达式1:表达式2 for 变量 in 序列 if 条件语句}
result={x:x*2 for x in range(4)}
print(result) #{0: 0, 1: 2, 2: 4, 3: 6}
dict1={'name':'张三','age':19}
result={x:None for x in dict1}
print(result) #{'name': None, 'age': None}
#交换字典中所有的键值对和值的位置:
dict2={'a':10,'b':20,'c':30}
result={dict2[x]:x for x in dict2}
print(result)
result={value:key for keu,value in dict2.items()}
定义一个变量保存一个学生的信息,学生信心中包括:姓名、年龄、成绩(单科)、电话、性别
students={
'name':'张',
'age':18,
'score':99,
'tel':'176728',
'gender':'男'
}
定义一个列表,在列表中保存6个学生的信息(学生信息中包括: 姓名、年龄、成绩(单科)、电话、性别(男、女、不明) )
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
sum=0
for x in students:
if x['score']<60:
sum+=1
print(sum)
打印不及格未成年学生的名字和对应的成绩
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
for x in students:
if x['score']<60 and x['age']<18:
print(x['name'],x['score'])
求所有男生的平均年龄
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
sum=0
for x in students:
sum+= x['age']
print(sum/len(students))
打印手机尾号是8的学生的名字
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
for x in students:
tel=list(x['tel'])
if tel[-1]=='8':
print(x['name'])
打印最高分和对应的学生的名字
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
max1=[]
max=0
for x in students:
if x['score']>max:
max=x['score']
max1.clear()
max1.append(x['score'])
max1.append(x['name'])
elif x['score']==max:
max1.append(x['score'])
max1.append(x['name'])
print(max1)
删除性别不明的所有学生
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
for x in students:
if x['gender']=='不明':
students.remove(x)
print(students)
将列表按学生成绩从大到小排序(挣扎一下,不行就放弃)
students=[{'name':'张','age':17,'score':98,'tel':'17287426728','gender':'男'},{'name':'李','age':23,'score':87,'tel':'17634283528','gender':'男'},
{'name':'王','age':21,'score':88,'tel':'17672842367','gender':'不明'},{'name':'欧阳','age':18,'score':72,'tel':'17674822834','gender':'女'},
{'name':'郑','age':17,'score':55,'tel':'17672568489','gender':'女'},{'name':'宋','age':19,'score':42,'tel':'17672893934','gender':'男'}]
max=0
for j in range(len(students)):
for x in range(len(students)-j-1):
if students[x]['score']<students[x+1]['score']:
m=students[x].copy()
students[x]=students[x+1]
students[x+1]=m
print(students)
定义一个变量保存一个班级的信息,班级信息中包括:班级名称、教室位置、班主任信息、讲师信息、班级所有的学生(根据实际情况确定数据类型和具体信息)
class1 = {
'name': 'Python2202',
'address': '14教室',
'lecturer': {'name': '余婷', 'qq': '726550822', 'gender': '女', 'age': 18},
'head_teacher': {'name': '舒玲', 'tel': '110', 'gender': '女', 'age': 18},
'students': [
{'name': '罗浩', 'tel': '120', 'gender': '男', 'major': '软件工程', 'age': 20, 'linkman': {'name': 'p1', 'tel': '29372'}},
{'name': 'stu1', 'tel': '62736', 'gender': '女', 'major': '电子信息', 'age': 23, 'linkman': {'name': 'p2', 'tel': '78221'}},
{'name': 'stu2', 'tel': '1292831', 'gender': '女', 'major': '会计', 'age': 27, 'linkman': {'name': 'p3', 'tel': '28239'}},
{'name': 'stu3', 'tel': '72289392', 'gender': '男', 'major': '数学', 'age': 21, 'linkman': {'name': 'p4', 'tel': '29102'}},
{'name': 'stu4', 'tel': '2737288', 'gender': '女', 'major': '计算机', 'age': 32, 'linkman': {'name': 'p5', 'tel': '236721'}},
{'name': 'stu5', 'tel': '27361028', 'gender': '男', 'major': '物理', 'age': 19, 'linkman': {'name': 'p6', 'tel': '34293'}},
{'name': 'stu6', 'tel': '829129', 'gender': '男', 'major': '石油', 'age': 22, 'linkman': {'name': 'p7', 'tel': '229311'}}
]
}
已知一个列表保存了多个狗对应的字典:
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': '母'}
]
利用列表推导式获取所有狗的品种
[‘银狐’, ‘法斗’, ‘土狗’, ‘哈士奇’, ‘银狐’, ‘土狗’]
breed=[x['breed'] for x in dogs]
print(breed)
利用列表推导式获取所有白色狗的名字
[‘贝贝’, ‘可乐’]
color=[x['name'] for x in dogs if x['color']=='白色']
print(color)
给dogs中没有性别的狗添加性别为 ‘公’
for x in dogs:
if 'gender'not in x:
x.setdefault('gender','公')
print(dogs)
统计 ‘银狐’ 的数量
sum=0
for x in dogs:
if x['breed']=='银狐':
sum+=1
print(sum)