set 集合 {} 无序
s = {1,2,3,4,5}
s = {}
print(type(s)) # 空{}就是字典
s = {1,2,3,4,5}
s = {1,2,3,'22','ss',False,(3,4)}
列表 和 字典 和 集合
print(s)
增
s.add(8)
print(s)
s.update('alex') # 迭代添加
print(s)
删
s = {1,2,3,'22','ss',(3,4)}
s1 = s.pop() #随机删除
print(s) #返回值 返回的被删除的元素
print(s1)
s.clear()
print(s) #清空 set()
s = {1,2,3,'22','ss',(3,4)}
del s #删除整个集合
print(s)
s = {1,2,3,'22','ss',(3,4)}
s.remove() #通过元素指定删除
print(s)
改:
s = {1,2,3,'22','ss',(3,4)}
s.remove(3)
s.add(7)
print(s)
查:
s = {1,2,3,'22','ss',(3,4)}
for i in s:
print(i)
s = {1,2,3,4,33,21,4,1234,34,1234,5324,1234,12341,23,22,34112,2,21,23,34,123,4}
集合天然去重
print(s)
面试题:
lst = [1,2,3,42,3,45,67,534,2]
print(list(set(lst)))
s = set(lst)
print(list(s))
# 其他操作:
s = {'黑哥'}
s1 = {'虎哥','开哥','alex哥','黑哥','wu哥'}
print(s & s1) # 交集 俩个集合都有的才输出
print(s | s1) # 并集 两个集合 合并到一起
print(s1 - s) # 差集 前面减后边的集合前边集合中有的输出出来 自己总结
print(s ^ s1) # 反交集 自己总结
print(s < s1) # 子集
print(s1 > s) # 超(父)集
dic = {{1,2,3}:1}
a = frozenset((1,2,3))
dic ={a:1}
print(dic)
print(dic)
lst = [1,2,3,[4,5,6],7]
lst1 = lst #赋值
lst2 = lst[:] # 切片
#
lst.append(7)
print(lst)
print(lst2)
import copy
lst4 = copy.deepcopy(lst) # 深拷贝
lst[-2].append(7)
print(lst) #[1, 2, 3, [4, 5, 6,7], 7]
print(lst4) #[1, 2, 3, [4, 5, 6], 7]
浅拷贝:
拷贝的是第一层元素
深拷贝:
拷贝所有元素开辟新的空间存放,完全没有关系
lst = [1,2,3]
lst1 = [1,2,3]
print(id(lst))
print(id(lst1))
面试题
a = [1, 2]
a[1] = a # [1,[1,2]]
print(a[1])
答案:
[1, [...]]
lst = [1,3,[5,7],9]
lst1 = lst
lst2 = lst[:]
# print(id(lst))
# print(id(lst1))
# print(id(lst2))
lst.append(9)
print(lst)
print(lst1)
print(lst2)
是不是一个东西 长的一不一样
lst = [1,2,3,4,5]
for i in lst:
lst.remove(i)
print(lst)
删除不行
lst = [1,2,3,4,5]
for i in lst:
lst.pop()
print(lst)
不行
lst = [1,2,3,4,5]
for i in lst:
lst.clear()
print(lst)
行,作弊
lst = [1,2,3,4,5]
for i in lst:
del lst[0]
print(lst)
不行
lst = [1,2,3,4,5]
for i in range(len(lst)):
lst.pop(0)
print(lst)
这个行
lst = [1,2,3,4,5]
for i in range(len(lst)):
lst.pop()
print(lst)
这个行
lst = [1,2,3,4,5]
for i in lst:
if type(i) == int:
lst[i-1] = ' '
print(lst)
print(list(set(lst)))
lst = [1,2,3,4,5]
lst1 = []
for i in lst:
lst1.append(i)
for k in lst1:
lst.remove(k)
print(lst)
这个方式也可以
for i in dic:
dic['c'] = 3
print(dic)
for i in dic:
dic.pop(i)
print(dic)
lst = {}
dic ={'a':1,'b':2}
for i in dic:
lst[i]= dic[i]
print(lst)
for i in lst:
dic.pop(i)
print(dic)
################################深坑###################################
dic = dict.fromkeys(['alex','wusir','wenzhou','nvshen','kaige'],['太白','姗姗'])
fromkeys 第一是键 第二是值
print(dic)
dic['alex'].append('老村长')
print(dic)
ascii # 不支持中文
unicode 万国码 中文4个字节 英文 2个
utf-8 最少使用1个字节 欧洲 2 亚洲 3
gbk 中文 2 英 1
windows 默认编码GBK
a = '你好黑'
a1 = 'alex'
a2 = a.encode('utf-8') #编码
print(a)
print(a2)
a3 = a2.decode('utf-8') #解码
print(a3)
bytes字节 用于传输和存储
a = '你好黑'
a1 = 'alex'
a2 = a.encode('gbk') #编码
a3 = a.encode('utf-8') # 最长用
print(a2)
print(a3)
a4 = a2.decode('gbk') #解码
a5 = a3.decode('utf-8')
print(a4,a5)
######坑########
lst = [1,2,3,4,5,[3,4,6,8],5]
lst1 = lst
lst1 = lst[:] # 浅拷贝只拷贝第一层元素
print(id(lst))
print(id(lst1))
lst.append(6)
print(lst)
print(lst1)
lst[-2].append(9)
print(lst)
print(lst1)
dic = {'a':1,'b':1}
dic1 = dic
print(id(dic1))
print(id(dic))
import copy
lst = [1,2,3,4,5,[3,4,6,8],5]
lst1 = copy.deepcopy(lst)
print(lst)
print(lst1)
lst[-2].append(10)
print(lst)
print(lst1)