set => 不允许重复的集合 set不允许重复 的列表
1、 创建
<span style="font-family:FangSong_GB2312;font-size:18px;">
s = set()
s = {1,2,3}
</span>
2、 转换
<span style="font-family:FangSong_GB2312;font-size:18px;">
l = [11,22,33,44,11]
l = (11,22,33,11)
l3 = "123"
s = set(l)
print(s)
</span>
---------------------------------------------------------------------------------------------------------------------------------------------------------
1、add 添加
def add(self,*args,**kwargs):
“””添加元素”””
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
print(se.add(44))
print(se)
结果:
None
{33, 11, 44, 22}
#------------------------------------------</span>
2、clear 清除
def clear(self,*args,**kwargs)
"""清除"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
print(se.clear())
print(se)
结果:
None
set()
#-------------------------------------------------</span>
3、copy复制(留在以后单独讲)#--------------------------------------
4、difference ---求差集
def difference(self,*args,**kwargs):
“”“A 中存在,b不存在”“”
#找se中存在,be中不存在的集合,并将其赋值给新变量
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
print(se.difference(be))
ret = se.difference(be)</span><pre name="code" class="python"><span style="font-family:FangSong_GB2312;font-size:18px;">se = {11,22,33}
be = {11,77,55}
ret = se.symmetric_difference(be)
print(ret)
结果:
{33, 77, 22, 55}
#--------------------------------------------------</span>
5、difference_update def difference_update(self,*args,**kwargs):“”“ 从当前集合中删除和B中相同的元素”“”#找se中存在,be中不存在的集合,更新自己案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.difference_update(be)
print(se)
结果:
{33, 22}
#-----------------------------------------------</span>
6、def symmetric_difference(self,*args,**kwargs):
"""对称交集"""
#意思就是说把A和B的不同元素赋值给一个新值
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
ret = se.symmetric_difference(be)
print(ret)
结果:
{33, 77, 22, 55}
#--------------------------------------------------</span>
7、def symmetric_difference_update(self,*args,**kwargs):"""对称交集,并更新到A中"""
#意思就是A和B中的不同元素,更新到A中
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.symmetric_difference_update(be)
print(se)
结果:
{33, 77, 22, 55}
#------------------------------------</span>
8、discard
def discard (self,*args,**kwargs):
"""移除指定元素,不存在不报错"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.discard(11)
print(se)
结果:
{33, 22}
#-------------------------------------</span>
9、intersectiondef intersection(self,*args,**kwargs):
"""交集"""
#意思就是A和B共同的
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
ret = se.intersection(be)
print(ret)
结果:
{11}
#------------------------------------------</span>
10、intersection_update
def interection_update(self,*args,**kwargs):
"""取交集并更新到A中 """
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.intersection_update(be)
print(se)
结果:
{11}
#----------------------------------------
</span>
11、isdisjoint
def isdisjoint(self,*args,**kwargs):
"""如果没有交集,返回True,否则返回False"""
#注意有交集是False
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
ret = se.isdisjoint(be)
print(ret)
答案:
False
#--------------------------------</span>
12、issubset
def issubset(self,*args,**kwargs):
"""是否是子序列"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">se = {11,22,33}
be = {11,77,55}
print(se.issubset(be))
答案:
False
#---------------------------------</span>
13、issuepersetdef issuperset(self,*args,**kwargs):
"""是否是父序列"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
print(se.issuperset(be))
答案:
False
#----------------------------------</span>
14、pop
def pop(self,*args,**kwargs):
"""移除元素"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.pop()
print(se)
se.pop()
print(se)
答案:
{11, 22}
{22}
证明set是无序的
#------------------------------------</span>
15、removedef remove(self,*args,**kwargs):
"""移除指定元素,不存在报错"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">se = {11,22,33}
be = {11,77,55}
se.remove(11)
print(se)
答案:
{33, 22}
#------------------------------------</span>
16、uniondef union(self,*args,**kwargs):
"""并集"""
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
ret = se.union(be) #——(be也可以是一个列表,字符串等)
print(ret)
答案:
{33, 22, 55, 11, 77}
#------------------------------------</span>
17、update
def update(self,*args,**kwargs):
"""更新"""
#A和B里面的值更新到A,且不重复
案例:
<span style="font-family:FangSong_GB2312;font-size:18px;">
se = {11,22,33}
be = {11,77,55}
se.update(be) #---必须可迭代
print(se)
答案:
{33, 22, 55, 11, 77}</span>