一、集合
set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
集合和我们数学中集合的概念是一样的,也有交集,并集,差集,对称差集等概念
1、集合的定义
定义一个集合需要提供一个列表作为参数,也可以不传入参数创建一个空的集合
>>> s = set([1, 2, 2, 3])
>>> s
{1, 2, 3} # 可以看到在创建集合对象对过程中已经为我们把重复的元素剔除掉了
>>> s = set()
set()
2、集合的常用方法
1)add——添加元素
代码:
def add(self, *args, **kwargs): # real signature unknown
"""
添加一个新的元素,如果该元素已经存在,将不会有任何操作
Add an element to a set. This has no effect if the element is already present.
"""
pass
示例:
>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}
2)clear——清空集合
代码:
def clear(self, *args, **kwargs): # real signature unknown
"""
删除所有元素
return:None
Remove all elements from this set.
"""
pass
示例:
>>> s = set([1, 2, 3])
>>> s.clear()
>>> s
set()
3)difference——差集
代码:
def difference(self, *args, **kwargs): # real signature unknown
"""
求当前差集和另一个集合的差集
return:差集
Return the difference of two or more sets as a new set. (i.e. all elements that are in this set but not the others.)
"""
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.difference(B)
{1}
说明:相当于数学中的差集概念一样,数学表示为A-B,如下图所示
4)difference_update——差集
代码:
def difference_update(self, *args, **kwargs): # real signature unknown
"""
从当前集合中删除所有另一个集合中存在的元素
其实就相当于将difference返回的值又付给了当前集合
可以理解为A = A - B,或者A -= B
Remove all elements of another set from this set. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.difference_update(A)
>>> A
{1}
5)discard——从集合中删除元素
代码:
def discard(self, *args, **kwargs): # real signature unknown
"""
如果一个几个集合中存在这个元素则删除,否则什么都不做
Remove an element from a set if it is a member. If the element is not a member, do nothing.
"""
pass
示例:
>>> A = set([1, 2, 3])
>>> A.discard(2)
>>> A
{1, 3}
>>> A.discard(4)
>>> A
{1, 3}
6)intersection——交集
代码:
def intersection(self, *args, **kwargs): # real signature unknown
"""
返回两个集合的交集到一个新的集合中
Return the intersection of two sets as a new set. (i.e. all elements that are in both sets.)
"""
pass
示例:
>>> A = set([1, 2, 3])
>>> A = set([2, 3, 4])
>>> A.intersection(A)
{2, 3}
说明:和数学中交集的概念一样,表示为A∩B,如下图所示
说明:和数学中一样A∩B = B∩A
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> B.intersection(A)
{2, 3}
7)intersection_update——交集
代码:
def intersection_update(self, *args, **kwargs): # real signature unknown
"""
求一个集合与另一个集合的交集,并把结果返回当前集合
Update a set with the intersection of itself and another. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.intersection_update(B)
>>> A
{2, 3}
8)isdisjoint——判断是否有交集
代码:
def isdisjoint(self, *args, **kwargs): # real signature unknown
"""
判断两个集合是否存在交集,有返回False,没有返回True
Return True if two sets have a null intersection. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.isdisjoint(B)
False
>>> A = set([1, 2, 3])
>>> B = set([6, 5, 4])
>>> A.isdisjoint(B)
True
注意:这里是没有交集返回True,有交集返回False,有点别扭,但不要搞反了
9)issuperset——判断当前集合是否是另一个集合的父集合
代码:
def issuperset(self, *args, **kwargs): # real signature unknown
"""
判断当前集合是否是另一个集合父集合(另一个集合的所有元素都在当前集合中)
Report whether this set contains another set. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([1, 2, 3, 4])
>>> A.issuperset(B)
False
>>> B.issuperset(A)
True
>>> A = set([1, 2, 3, 4])
>>> B = set([1, 2, 3])
>>> A.issuperset(B)
True
>>> B.issuperset(A)
False
>>> B = set([1, 2, 3, 4])
>>> A.issuperset(B)
True
>>> B.issuperset(A)
True
说明:相当于数学中的A ⊆ B 如下图所示。注意,包括等于的情况
10)pop——随机删除元素,并返回删除的元素
代码:
def pop(self, *args, **kwargs): # real signature unknown
"""
随机删除一个元素,并返回那个元素,如果集合为空,将会抛出KeyError异常
Remove and return an arbitrary set element.
Raises KeyError if the set is empty.
"""
pass
示例:
>>> s = set([1, 2, 3])
>>> s.pop()
1
11)remove——删除一个元素
代码:
def remove(self, *args, **kwargs): # real signature unknown
"""
从集合中删除一个元素,如果要删除的元素不在集合,将会抛出KeyError异常
Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
"""
pass
示例:
>>> s = set([1, 2, 3])
>>> s.remove(2)
>>> s
{1, 3}
12)symmetric_difference——返回两个集合的对称差集
代码:
def symmetric_difference(self, *args, **kwargs): # real signature unknown
"""
返回两个集合的对称差集到一个新的集合
Return the symmetric difference of two sets as a new set. (i.e. all elements that are in exactly one of the sets.)
"""
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4, 5])
>>> A.symmetric_difference(B)
{1, 4, 5}
>>> B.symmetric_difference(A)
{1, 4, 5}
说明:对称差集和数学中的概念一样,如下图所示,另外A △B == A △B
13)symmetric_difference_update——当前集合更新为与另一个集合的对称差集
代码:
def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
"""
更新当前集合为与另外一个集合的对称差集
Update a set with the symmetric difference of itself and another. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4, 5])
>>> A.symmetric_difference_update(B)
>>> A
{1, 4, 5}
14)union——返回与另一个集合的并集为一个新的集合
代码:
def union(self, *args, **kwargs): # real signature unknown
"""
返回与另一个集合的并集为一个新的集合
Return the union of sets as a new set. (i.e. all elements that are in either set.)
"""
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4, 5])
>>> A.union(B)
{1, 2, 3, 4, 5}
>>> B.union(A)
{1, 2, 3, 4, 5}
说明:与数学中并集的概念一样,如下图所示,并且A∪B == B∪A
15)update——更新当前集合为与另一个集合的并集
代码:
def update(self, *args, **kwargs): # real signature unknown
"""
更新当前集合为与另一个集合的并集
Update a set with the union of itself and others. """
pass
示例:
>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4, 5])
>>> A.update(B)
>>> A
{1, 2, 3, 4, 5}