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