class set(object):
def add(self, *args, **kwargs): # 添加一项
>>> a
{'3', '1', '2'}
>>> a.add(4)
>>> a
{'3', 4, '1', '2'}
def clear(self, *args, **kwargs): # 清除所有
>>> a
{'3', 4, '1', '2'}
>>> a.clear()
>>> a
set()
def copy(self, *args, **kwargs): # 返回一个集合的浅拷贝
def difference(self, *args, **kwargs): # 返回一个a中有但b中没有的元素新集合
c = a-b;(注意与difference_update的区别)
>>> b
{'4', '6', '5'}
>>> a
{'4', '3', '5', '1', '2'}
>>> a.difference(b)
{'3', '1', '2'}
def difference_update(self, *args, **kwargs): # 返回删除了b中所有元素后的a集合,
a = a-b;
>>> a
{'4', '3', '1', '2'}
>>> b
{'4', '6', '5'}
>>> a.difference_update(b)
>>> a
{'3', '1', '2'}
def discard(self, *args, **kwargs): # 如果集合中存在某一元素则删除,没有也不会报错;
>>> a
{'4', '3', '5', '1', '2'}
>>> a.discard(x)
>>> a
{'4', '3', '5', '1', '2'}
def intersection(self, *args, **kwargs): # 取交集,新创建一个set
>>> a.intersection(b)
{'5'}
>>> a
{'5'}
def intersection_update(self, *args, **kwargs): #a中保留有交集元素,不创建一个新set;
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '6', '5'}
>>> a.intersection(b)
{'4', '5'}
>>> a
{'4', '3', '5', '1', '2'}
def isdisjoint(self, *args, **kwargs): # 如果没有交集返回true,否则返回false;
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '6'}
>>> a.isdisjoint(b)
False
>>> b.remove('4')
>>> b
{'6'}
>>> a.isdisjoint(b)
True
>>>
def issubset(self, *args, **kwargs): # 是否为父级
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '5'}
>>> a.issuperset(b)
True
def issuperset(self, *args, **kwargs): # 判断b是否为a子集,如果是为True 否则false;
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '5'}
>>> a.issuperset(b)
True
def pop(self, *args, **kwargs): # 移除不需要加参数取最后一个元素,并在集合中删除该值
如果为空,则返回keyerror
>>> a.update([6,7])
>>> a
{6, 7, '2', '5', '1'}
>>> a.pop()
6
>>> a.pop()
7
def remove(self, *args, **kwargs): # 需要加参数并且没有返回值,在集合中删除该元素;
>>> a
{'4', '6', 5, 6, '3', '5', '1', '2'}
>>> a.remove(5)
>>> a
{'4', '6', 6, '3', '5', '1', '2'}
def symmetric_difference(self, *args, **kwargs): # 返回一个两者之一有的元素集合
并创建一个新的集合 c=a^b ;
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '6', '5'}
>>> a.symmetric_difference(b)
{'2', '6', '3', '1'}
def symmetric_difference_update(self, *args, **kwargs): # 返回一个两者之一有的元素集合
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '6', '5'}
>>> a.symmetric_difference_update(b)
>>> a
{'2', '6', '3', '1'}
def union(self, *args, **kwargs): # 返回一个新的集合,包含a,b中每一个元素;c=a+b
>>> a
{'4', '3', '5', '1', '2'}
>>> b
{'4', '6', '5'}
>>> a.union(b)
{'2', '4', '6', '3', '5', '1'}
def update(self, *args, **kwargs): # 可同时添加多个元素,而add只可同时添加一个;
>>> a
{'2', '5', '1'}
>>> a.update([3,4])
>>> a
{3, 4, '2', '5', '1'}