07 set集合,深浅拷⻉以及部分知识点补充
一.while,for 循环知识点补充
二.int, str的相关操作
1.列表变字符串
# lst=["红","橙","黄","绿","青","蓝","紫"]
方法一 # s="_".join(lst)
# print(s)
方法二 # sum=""
# for i in lst: # sum=sum+i+"_" # print(sum) # 字符串连接
# s="金","木","水","火","土" # s1="_".join(s) # print(s1)
元组连接 # tu=("男","女","老","少") # tu1="_".join(tu) # print(tu1) 2 list删除问题
# lst=["红","橙","黄","绿","青","蓝","紫"]
方法一 clear
# lst.clear() # print(lst)
方法二 del range(0,len(lst))
# for i in range(0,7): # del lst[0] # print(lst) 方法三 pop range(0,len(lst))
# for i in range(0,7): # lst.pop(0) # print(lst)
方法四 把要删除的元素保存在新列表中,循环新列表,删除旧列表.
# lst1=[] # for i in lst: # lst1.append(i) # for el in lst1: # lst.remove(el) # print(lst)
3 dict 中的fromkey( )
#fromkey : 第一个参数中每一个拿出来和第二个参数组织成键值对. #注意代码中只是更改了jay那个列表. 但是由于jay和JJ⽤的是同⼀个列表. 所以. 前⾯那个改了. 后⾯那个也会跟着改.
# dic=dict.fromkeys(["jay","jj"],["周杰伦","林俊杰"]) # dic.get("jay").append("蔡依林") # print(dic)
4. dic中的元素在迭代过程中是不可以删除的.,需要把要删除的内容存在新列表中,循环新列表,删除旧列表
# lst=["红","橙","黄","绿","青","蓝","紫"] # lst1=["黄","绿"] # for el in lst1: # lst.remove(el) # print(lst)
5. set() 集合 是dict类型的数据,只存key的值,不存value的值.
特点: 元素不重复(应用"去重");无序;内容必须不可变(可哈希);set本身是可变的(增删改).
去重:
# lst=["红","橙","黄","绿","青","蓝","紫","青","绿"] # s=set(lst) # print(s)
set 是可变的,里面内容是不可变的
# s={"alex",1,True,{"你好"}} 不可行,因为{"你好"}不可哈希( 可变的) #s={"麻花",[1,2,3,]} 不可行 因为[1,2,3]是列表,列表不可哈希( 可变的) #s={"听","说","读","写",(1,3)} 可行 因为(1,3)是元组,元组是不可变的(可哈希)
6.set 增 删 改 查
增一
# s=set() # s.add("大神") # print(s)
增二 update迭代更新
# s={"赵","钱","孙","李","张","王"} # s.update("吉孔孟") # print(s) # s.update(["中国"]) # print(s) # s.update(["中","国"]) # print(s)
删一 pop() 随机删除
# s={"赵","钱","孙","李","张","王"} # s1=s.pop() # print(s1) 删除的元素 # print(s) 删除以后的结果
# 删二clear 打印出来是set()
删三remove
# s.remove("王") 删除指定元素 # print(s)
#修改 set无法直接修改,先删掉,再增加.
# s={"赵","钱","孙","李","张","王"} # s.remove("王") # s.add("吉") # print(s)
查询
# s={"赵","钱","孙","李","张","王"} # for el in s: # print(el)
7. 其他操作
#交集 print(s1 and s2)
# s1={"赵","钱","孙","李","张","王"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1&s2) # print(s1.intersection(s2))
并集
# s1={"赵","钱","孙","李","张","王"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1|s2) # print(s1.union(s2))
差集 print(s1-s2) 得到第一个中单独存在的
# s1={"赵","钱","孙","李","张","王","杜"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1-s2) # print(s1.difference(s2))
反交集 两个集合中单独存在的数据 (和交集相反)
# s1={"赵","钱","孙","李","张","王","杜"} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1^s2) # print(s1.symmetric_difference(s2))
#子集 print(s1
# s1={"赵","钱","孙","李","张","王",} # s2={"赵","钱","孙","李","张","王","吉","宋"} # # print(s1
超集 print(s1
# s1={"赵","钱","孙","李","张","王",} # s2={"赵","钱","孙","李","张","王","吉","宋"} # print(s1>s2) # print(s1.issuperset(s2))
8. set集合本身是可以发⽣改变的. 是不可hash的. 我们可以使⽤frozenset来保存数据.frozenset是不可变的. 也就是⼀个可哈希的数据类型
# s1={"赵","钱","孙","李","张","王",} # s=frozenset(s1) # dic={s1:"123"} # print(dic)
#9.深浅拷贝 (1)赋值:对于list, set, dict来说, 直接赋值. 其实是把内存地址交给变量. 并不是复制⼀份内容. 所以.lst1的内存指向和lst2是⼀样的. lst1改变了, lst2也发⽣了改变
list列表
# lst1=["红","橙","黄","绿","青","蓝","紫"] # lst2=lst1 # print(lst1) # print(lst2) # lst1.append("粉") # print(lst1) # print(lst2)
ict字典
# dic1={"颜色":"红色","水果":"香蕉"} # dic2=dic1 # print(dic1) # print(dic2) # dic1["蔬菜"]="番茄" # print(dic1) # print(dic2)
(2)浅拷贝 只拷贝第一层,第二层的内容不拷贝
# lst1=["风","雨","雪","霜"] # lst2=lst1.copy() # lst1.append("冰雹") # print(lst1) # print(lst2) # print(id(lst1),id(lst2)) 2 # lst2=["风","雨","雪","霜",["电","雷"]] # lst3=lst2.copy() # print(lst3) # lst2[4].append("冰雹") # print(lst2) # print(lst3)
(3)深拷贝 把元素内部的元素完全进⾏拷⻉复制. 不会产⽣⼀个改变另⼀个跟着改变的问题
# import copy # lst1=["番茄","白菜","胡萝卜",["苹果","荔枝","香蕉"]] # lst2=copy.deepcopy(lst1) # print(lst1) # print(lst2) # lst1[3].append("芒果") # print(lst1) # print(lst2)
.
list的删除问题
四.set 的集合
五深浅拷贝