课程:字典
目标
- 字典的应用场景
- 创建字典的语法
- 字典常见的操作
- 字典的循环遍历
一、字典的应用场景
思考1:如果有多个数据,列如:‘tom’,‘男’,20 ,如果快速储存?
答:列表
list1 = ['tom','男',20]
思考2:如何查找到数据'tom'?
答:查找到下标为0的数据即可。
list1[0]
思考3:如果将来数据顺序发生变化,如下所示,还能用list1[0] 访问到数据'tom'吗?
list1 = ['男',20,'tom']
答:不能,数据'tom'此时下标为2,
思考4:数据顺序发生变化,每个数据的下标也随之变化,如何保证数据顺序变化前后能使用同意的标准查找数据呢??
答:字典,字典里面的数据是以键值对形式出现,字典数据和数据顺序没有关系,即字典不支持下标,后期无论数据如何变化,只需要按照对应的键的名字查找数据即可。
二、创建字典的语法
字典特点:
- 符号为大括号
- 数据为键值对形式出现
- 各个键值对之间用逗号隔开
# 1.有数据的字典: name的值TOM ,age的值是20,gender的值是男
dict1 = {'name':'tom','age':20,'gender':'男'}
print(dict1)
# 2. 创建空字典
dict2 ={}
dict3 = dict()
⭐注意:一般称冒号前面的键(key)简称k;冒号后面的为值(yalue)简称y
三、字典的常见操作
3.1增
写法:字典序列[key] = 值
⭐注意:如果key存在则修改这个key对应的值;如果key不存在则新增此键值对。
dict1 = {'name':'tom','age':20,'gender':'男'}
dict1['id'] = 110
#结果:{'name': 'tom', 'age': 20, 'gender': '男', 'id': 110}
print(dict1)
dict1['name'] = 'Rose'
#结果:{'name': 'Rose', 'age': 20, 'gender': '男', 'id': 110}
print(dict1)
⭐注意:字典为可变类型
3.2 删
- del() / del:删除字典或删除字典中指定的键值对
dict1 = {'name':'tom','age':20,'gender':'男'}
# del dict1['name']
结果:{'age': 20, 'gender': '男'}
# print(dict1)
del dict1['names'] #报错
- clear():清空字典
dict1 = {'name':'tom','age':20,'gender':'男'}
dict1.clear()
print(dict1) #{}
3.3改
写法:字典序列[key] = 值
⭐注意:如果key存在则修改key对应的值;如果key不存在则新增此键值对
dict1 = {'name':'tom','age':20,'gender':'男'}
dict1['name'] = 'lily'
# 结果:{'name': 'lily', 'age': 20, 'gender': '男'}
print(dict1)
dict1['id'] = 10
#结果:{'name': 'lily', 'age': 20, 'gender': '男', 'id': 10}
print(dict1)
3.4 查
3.4.1 key值查找
dict1 = {'name':'tom','age':20,'gender':'男'}
print(dict1['name']) # tom
print(dict1['id']) # 报错
⭐ 如果当前查找的key存在,则返回对应的值;否则则报错
3.4.2 get()
- 语法
字典序列.get(key,默认值)
⭐ 注意:如果当前查找的key不存在则返回第二个参数(默认值),如果省略第二个参数,则返回none
- 快速体验
dict1 = {'name':'tom','age':20,'gender':'男'}
print(dict1.get('name')) # tom
print(dict1.get('names')) # none
print(dict1.get('x',110)) # 110
3.4.3 keys()
dict1 = {'name':'tom','age':20,'gender':'男'}
结果:dict_keys(['name', 'age', 'gender'])
print(dict1.keys())
⭐ 查找字典中所以的key,返回可迭代对象-----可用将来遍历的对象
3.4.4 values()
dict1 = {'name':'tom','age':20,'gender':'男'}
#结果:dict_values(['tom', 20, '男'])
print(dict1.values())
⭐查找字典中所以的value.返回可迭代对象
3.4.5 items (键值对----元素)
dict1 = {'name':'tom','age':20,'gender':'男'}
#结果:dict_items([('name', 'tom'), ('age', 20), ('gender', '男')])
print(dict1.items())
⭐ 查找字典中所有的键值对,返回可迭代的对象, 里面数据都是元组.元组数据1是字典的key. 元组数据2是字典的key对应的值
四、字典的循环遍历
4.1 遍历字典的key
dict1 = {'name':'tom','age':20,'gender':'男'}
for key in dict1.keys():
print(key)
结果:
4.2 遍历字典的value
dict1 = {'name':'tom','age':20,'gender':'男'}
for value in dict1.values():
print(value)
结果:
4.3遍历字典的元素
dict1 = {'name':'tom','age':20,'gender':'男'}
for item in dict1.items():
print(item)
结果:
4.4遍历字典的键值对(拆包)
dict1 = {'name':'tom','age':20,'gender':'男'}
for key,value in dict1.items():
print(f'{key}={value}')
结果:
五、总结
- 定义字典
dict1 = {'name':'tom','age': 20}
dict2 = {}
dict3 = dict()
- 常见操作
- 增 / 改
字典序列[key] = 值
- 查找
- 字典序列[key]
- keys()
- values()
- items()
课程:集合
目标
- 创建集合
- 集合数据的特点
-
集合的常见操作
一,创建集合
- 创建集合使用 {} 或 set(),但是如果要创建空集合只能使用set(),因为{ }用来创建空字典。
- 集合没有下标,切排序顺序无规律,有去重复功能
s1 = {10,20,30,40,}
print(s1)
s2 = s1 = {10,20,10,30,10,40,}
print(s2)
s3 = set('abcdef')
print(s3)
print(type(s3))
s4 = set()
print(s4)
print(type(s4))
s5 = {}
print(type(s5))
结果:
二、集合常见操作方法
2.1增加数据
- add() ---追加单个数据
s1 = {10,20}
s1.add(1100)
s1.add(10)
print(s1) #{10, 20, 1100}
# s1.add([10,20,30]) #报错
# print(s1)
因为集合有去重功能,所以,当集合内追加得数据是当前集合已有数据得话,则不进行任何操作
- update(),追加得数据都是序列。
s1 = {10,20}
# s1.update(100) # 报错
s1.update([10,20,30])
s1.update('abc')
print(s1)
结果:
2.2删除数据
- remove(),删除集合中得指定数据,如果数据不存在则报错
s1 = {10,20,30,40,50}
s1.remove(10)
print(s1)
s1.remove(10) # 报错
print(s1)
- discrd(),删除集合中指定数据,如果数据不存在不会报错
s1 = {10,20,30,40,50}
s1.discard(10)
print(s1)
s1.discard(10)
print(s1)
- pop(),随机删除集合得某个数据,并返回这个数据
s1 = {10,20,30,40,50}
del_num = s1.pop()
print(del_num)
print(s1)
2.3 查找数据
- in:判断数据在集合序列
- not in :判断数据不在集合序列
s1 = {10,20,30,40,50}
print(10 in s1)
print(10 not in s1)
三,总结
- 创建集合
● 有数据集合
s1 = {数据1,数据2,.....}
● 无数据集合
s2 = set()
- 常见操作
● 增加数据
● add()
● update()
● 删除数据
● remove()
● discard()