一.集合(可变数据类型,但是元素必须是不可变的数据类型,无序,元素具有唯一性)
add -- 给集合添加一个元素
clear -- 清空一个集合 ==> set()
copy -- 返回一个浅拷贝集合
difference
set1 = {1,2,3,4}
set2 = {2,3,4,5}
set1.difference(set2) ==> {1} # 返回set1有set2中没有的元素组成的集合
print(set1 - set2) ==> {1} 同上
difference_update
set1.difference_update(set2)
print(set1,set2) ==> {1} {2,3,4,5} # 删除set1中有,同时set2中也有的元素
intersection # 返回两个集合的交集
set2.intersection_update(set1) # 删除set2中同时存在与set1中的元素
isdisjoint #
set2.issubset(set1) # 判断set2是否是set1的子集
set2 < set1
set1.issuperset(set2) # 判断set1是否包含set2的所有元素
set1 > set2
pop # 弹出集合元素
remove # 删除一个指定的元素
symmetric_difference #返回两个集合中独有的元素组成的集合
set1.symmetric_difference_update(set2) # 用两个集合中独有的元素组成的集合替换set1
union # 返回两个集合的并集
set1 | set2
set1.update(set2) # 用两个集合的并集替换前一个集合
二.深浅拷贝
python中一切皆对象,变量存储的是对象的内存地址,每做一次赋值操作,都开辟一块新的内存
空间存放值,同时将内存地址赋值给变量(小数据池范围内除外)
str1 = 'hello'
str2 = 'HELLO'
print(id(str1),id(str2)) ==> 1565926888424 1565926888256
--------------针对深拷贝-----------------------------------------------------------
对于复杂数据结构来说,增删改数据结构内部元素并不会引起整个数据结构内存地址的改变
只影响所修改的元素的内存地址
lst = [1,3,4,5]
print(lst,id(lst)) ==> [1, 3, 4, 5] 1565926868744
lst.append(6)
print(lst,id(lst)) ==> [1, 3, 4, 5, 6] 1565926868744
lst.pop(3)
print(lst,id(lst)) ==> [1, 3, 4, 6] 1565926868744
lst[0] = '修改为一个字符串'
print(lst,id(lst)) ==> ['修改为一个字符串', 3, 4, 6] 1565926868744
复杂数据结构的赋值操作,多个引用指向同一个内存地址,通过任意变量修改数据结构,其它
变量的访问同时受到影响
lst1 = lst
lst.append(7)
print(lst,id(lst),lst1,id(lst1)) ==> [0, 3, 4, 6, 7] 1565926868744 [0, 3, 4, 6, 7] 1565926868744
如果需要赋值一份独立的数据,需要用到拷贝
深拷贝:复制一份完全一模一样的数据,新数据与原数据内存地址不同
import copy
slst = [1,2,[3,4,[5,6]]]
clst = copy.deepcopy(slst)
print(id(slst),id(clst)) ==> 1565926868488 1565928337544
数据结构和所有元素均一样,元素未修改的时候和原数据结构相同,一旦发生修改,则新开辟一块内存空间存储新值
print(id(slst[0]),id(clst[0])) ==> 1886940176 1886940176
slst[0] = 'change'
print(id(slst[0]),id(clst[0])) ==> 1565926889936 1886940176
深拷贝不管有几层嵌套,修改元素新开辟内存空间均只影响本数据结构
--------------针对浅拷贝-----------------------------------------------------------
复杂数据结构执行浅拷贝操作,整体内存地址发生改变
slst = [1, 2, [3, 4, [5, 6]]]
print(id(slst),id(nclst)) ==> 1565926868488 1565928339144
第一层元素未变化时,内存地址相同,发生变化后,新开辟一块内存存放新内容,只影响
本数据结构
print(id(slst[0]),id(nclst[0])) ==> 1565926889936 1565926889936
slst[0] = 'nchange'
print(id(slst[0]),id(nclst[0])) ==> 1565926889992 1565926889936
第二层以后的元素发生改变时,开辟新空间,同时原数据结构和复制的数据结构同时指向新开辟的内存地址
print(slst[2][0],id(slst[2][0]),nclst[2][0],id(nclst[2][0])) ==> 3 1886940240 3 1886940240
slst[2][0] = 'schange'
print(slst[2][0],id(slst[2][0]),nclst[2][0],id(nclst[2][0])) ==> schange 1565926888592 schange 1565926888592
nclst[2][0] = 'ncchange'
print(slst[2][0],id(slst[2][0]),nclst[2][0],id(nclst[2][0])) ==> ncchange 1565926904560 ncchange 1565926904560
三.补充(删除列表中的元素列表的索引发生变化)
lst = [x for x in range(10)]
>>> for i in range(len(lst)):
... print(i)
... del lst[i]
... print(lst)
...
0
[1, 2, 3, 4, 5, 6, 7, 8, 9]
1
[1, 3, 4, 5, 6, 7, 8, 9]
2
[1, 3, 5, 6, 7, 8, 9]
3
[1, 3, 5, 7, 8, 9]
4
[1, 3, 5, 7, 9]
5
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
IndexError: list assignment index out of range