一、集合:类似列表,元组的存储数据容器,不同点是不可修改,不可重复.无序排列.
1.创建集合:
(1).set1 = {'abby', 'eric'}
result:{'eric', 'abby'}
(2).set2 = set({'abby', 'eric'})
result:{'abby', 'eric'}
2.增
set1 = {'abby','eric','allan','davy','skye'}
(1).set1.add('west')
result:{'west','abby','eric','allan','davy','skye'}
(2).set1.update('abc')#迭代新增
result:{'west','abby','a','eric','allan','c','davy','b','skye'}
3.删
set1 = {'abby','eric','allan','davy','skye'}
(1).set1.remove('abby')#指定删除
result:{'eric','allan','davy','skye'}
(2).set1.pop()#随机删除
result:{'abby','eric','allan','skye'}
(3).set clear()#清空集合
result:set()
(4).del set1#从内存删除集合
result:name 'set1' is not defined
4.关系测试:
set1 = {1,2,3,4,5}
set2 = {4,5,6,7,8}
(1).交集:
1:print(set1 & set2)
2:print(set1.intersection(set2)
result:{4,5}
(2).并集
1:print(set1 | set2)
2:print(set1.union(set2)
result:{1, 2, 3, 4, 5, 6, 7, 8}
(3).反交集
1:print(set1 ^ set2)
2:print(set1.symmetric_difference(set2)
result:{1, 2, 3, 6, 7, 8}
(4).差集
1:print(set1 - set2)
2:print(set1.difference(set2)
result:{1, 2, 3}
3.print(set2-set1)
4.print(set2.difference(set1)
result:{8, 6, 7}
(5)子集
set1 = {1,2,3}
set2 = {1,2,3,4,5,6}
print(set1 < set2)
print(set1.issubset(set2))
result:True
(5)超集
print(set2 > set1)
print(set2.issuperset(set1))
result:True
(6)冻结,不可变的数据类型
set1 = {1,2,3}
set3 = frozenset(set1)
result:frozenset({1, 2, 3})
面试题:list去重
l1 = [1,1,2,3,4,4,3,2,1,5,5]
set1 = set(l1)
l2 = list(set1)
print(l2)
二、深浅copy
1. 赋值运算
l1 = [1,2,3]
l2 = l1
l1.append(666)
print(l2)
print(id(l1))
print(id(l2))
result:
[1, 2, 3, 666]
2724735509128
2724735509128
2.浅copy
test01:
l1 = [1,2,3]
l2 = l1.copy()
l1.append(666)
print(l2)
result:[1, 2, 3]
test02:
l1 = [1,2,3,[22,]]
l2 = l1.copy()
l1.append(666)
print(l1,l2)
result:[1, 2, 3, [22], 666] [1, 2, 3, [22]]
test03:
l1 = [1,2,3,[22,]]
l2 = l1.copy()
l1[-1].append('abby')
print(l1,l2)
print(id(l1))
print(id(l2))
print(id(l1[-1]))
print(id(l2[-1]))
result:
[1, 2, 3, [22, 'abby']] [1, 2, 3, [22, 'abby']]
2586054582984
2586055807688
2586054582920
2586054582920
3.深copy
需要copy模块
import copy
l1 = [1,2,3,[22,]]
l2 = copy.deepcopy(l1)
l1.append(666)
print(l2)
result:[1, 2, 3, [22]]
总结深浅copy:都是copy,浅copy复制第一层独立存在,第二层与复制对象共同拥有.深copy复制所有层并且独立存在
面试题:
l1 = [1,2,3,[22,33]]
l2 = l1[:]
l1.append(666)
print(l2)
result:[1, 2, 3, [22, 33]]
总结:切片属于浅copy
三、基础数据补充
1.数据类型的转换
# str ---> list
s1 = 'abby eric west'
l1 = s1.split()
print(l1)
result:['abby', 'eric', 'west']
l1 = ['abby', 'eric', 'west']
s2 = ' '.join(l1)
print(s2)
result:abby eric west
# tuple <---> list
l1 = [1,2,3]
tu1 = tuple(l1)
print(tu1)
result:(1, 2, 3)
tu2 = (0,2,3)
l1 = list(tu2)
print(l1)
result:[0, 2, 3]
# dic ---> list
dic1 = {'name': 'alex', 'age': 1000}
l1 = list(dic1)
print(l1)
result:['name', 'age']
bool转换
0 " [] () {} set() ---> bool: False
四、enumerate枚举
字典用法:
dic01 = {'key1': 'value1','key2': 'value2', 'k3':'v3', 'name': 'abby'}
1.for i in enumerate(dic01,1):#1为索引起始位
print(i)
result:
(1, 'key1')
(2, 'key2')
(3, 'k3')
(4, 'name')
2.for a,b in enumerate(dic01,1):
print(a,b)
result:
1 key1
2 key2
3 k3
4 name
列表:li01 = [1,2,3,4,5,6,7,8]
for i in enumerate(li01,1):
print(i)
result:
(1, 1)
(2, 2)
(3, 3)
(4, 4)
(5, 5)
(6, 6)
(7, 7)
(8, 8)
for a,b in enumerate(li01,1):
print(a,b)
result:
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8