01.回顾
1.字典:
查询速度快,存储大量的关联数据
键:必须是不可变的数据类型(int str bool tuple)
值:任意数据类型,对象
2.增删改查
增:setdefualt(),dic['age'] = 18
删:pop 键(可以设置返回值),clear 清空,del dic['name']
改:dic['name'] = 'hhh'
查:dic['name'] dic.get('name') dic.keys() dic.values() dic.items()
3.字典的嵌套
02. id is ==
# id 身份证号
# i = 100
# s = 'hhh'
# print(id(i))
# print(id(s))
# == 比较两边的值是否相等
# l1 = [1,2,3]
# l2 = [1,2,3]
# print(l1 == l2)
# s1 = 'hhh'
# s2 = 'hhh '
# print(s1 == s2)
# is 判断内存地址是否相同
# l1 = [1,2,3]
# l2 = [1,2,3]
# print(id(l1))
# print(id(l2))
# print(l1 is l2)
s1 = 'hhh'
s2 = 'hhh'
print(id(s1))
print(id(s2))
print(s1 is s2)
# id 相同,值一定相同
# 值相同,id不一定相同
03.代码块
代码块:我怕们所有的代码都需要依赖代码块执行。
一个文件就是一个代码块
交互式命令下一行就是一个代码块
04两个机制:
同一个代码块下,有一个机制。不同的代码块下,遵循另一个机制
同一个代码块下的缓存机制
前提条件:同一个代码块内
机制内容
适用对象:int bool str
具体细则:所有的数字,bool,几乎所有的字符串
i1 = 1000
i2 = 1000
i3 = 1000
print(id(i1))
print(id(i2))
print(id(i3))
l1 = [1,2,3]
l2 = [1,2,3]
print(id(l1))
print(id(l2))
优点:提升性能,节省内存。
不同代码块的缓存机制:小数据池
前提条件:同一个代码块内
机制内容:
适用对象:int bool str
具体细则:-5~256数字,bool,满足规则的字符串。
优点:提升性能,节省内存。
05.集合 set
容器型的数据类型,它要求它里面的元素是不可变的数据,但是它本身是可变的数据类型。集合是无序的。{}。
集合的作用:
列表的去重。
关系测试:交集并集,差集....
# 集合的创建
# set1 = set({1, 3, 'hhh', False})
# set1 = {1, 3, 'zs', 4, 'ls', False, 'ww'}
# print(set1) # 无序
# 空集合:
# print({}, type({})) # 空字典
# set1 = set() # 空集合
# print(set1,type(set1))
# 集合有效性测试
# set1 = ({[1,2,3], 3, {'name': 'zs'}})
# print(set1) # 报错
# 增
set1 = {'zs', 'ls', 'ww', 'zl', 'kk', 'tt'}
# add
# set1.add('xx')
# print(set1)
# updata迭代的增加
# set1.update('asdf')
# print(set1)
# 删
# set1 = {'zs', 'ls', 'ww', 'zl', 'kk', 'tt'}
# remove
# set1.remove('ww')
# print(set1)
# pop 随机删除
# set1.pop()
# print(set1)
# 变相改值
# set1 = {'zs', 'ls', 'ww', 'zl', 'kk', 'tt'}
# set1.remove('ww')
# set1.add('hhh')
# print(set1)
# 交集 (& 或 intersection)
# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# print(set1 & set2)
# 关系测试: ***
# 并集 (|或 union)
# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# print(set1 | set2)
# 差集 (- 或 difference)
# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# print(set1 - set2)
# 反交集 (^ 或 symmetric_difference)
# set1 = {1,2,3,4,5}
# set2 = {4,5,6,7,8}
# print(set1 ^ set2)
# 子集
# set1 = {1,2,3}
# set2 = {1,2,3,4,5,6}
# print(set1 < set2)
# 超集
# set1 = {1,2,3}
# set2 = {1,2,3,4,5,6}
# print(set2 > set1)
# 列表去重 # 不保持数据 ***
li = [1, 1, 2, 'hhh', 2, 2, 'hhh', 3, 4, 5, 6, 'hhh', 6, 6]
set1 = set(li)
li = list(set1)
print(set1)