我的Python成长之路---第三天---Python基础(9)---2016年1月16日(*)

一、集合

    set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。

    集合和我们数学中集合的概念是一样的,也有交集,并集,差集,对称差集等概念

    1、集合的定义

    定义一个集合需要提供一个列表作为参数,也可以不传入参数创建一个空的集合 

>>> s = set([1, 2, 2, 3])
>>> s
{1, 2, 3} # 可以看到在创建集合对象对过程中已经为我们把重复的元素剔除掉了
>>> s = set()
set()

    2、集合的常用方法

    1)add——添加元素

    代码:

1 def add(self, *args, **kwargs): # real signature unknown
2         """
3         添加一个新的元素,如果该元素已经存在,将不会有任何操作
4         Add an element to a set.
5         
6         This has no effect if the element is already present.
7         """
8         pass

     示例:

>>> s = set([1, 2, 3])
>>> s
{1, 2, 3}
>>> s.add(4)
>>> s
{1, 2, 3, 4}

     2)clear——清空集合

    代码:

1 def clear(self, *args, **kwargs): # real signature unknown
2         """ 
3         删除所有元素
4         return:None
5         Remove all elements from this set. 
6         """
7         pass

    示例:

>>> s = set([1, 2, 3])
>>> s.clear()
>>> s
set()

     3)difference——差集

     代码:

1 def difference(self, *args, **kwargs): # real signature unknown
2         """
3         求当前差集和另一个集合的差集
4         return:差集
5         Return the difference of two or more sets as a new set.
6         
7         (i.e. all elements that are in this set but not the others.)
8         """
9         pass

     示例:

>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.difference(B)
{1}

     说明:相当于数学中的差集概念一样,数学表示为A-B,如下图所示

    4)difference_update——差集

    代码:

1 def difference_update(self, *args, **kwargs): # real signature unknown
2         """ 
3         从当前集合中删除所有另一个集合中存在的元素
4         其实就相当于将difference返回的值又付给了当前集合
5         可以理解为A = A - B,或者A -= B
6         Remove all elements of another set from this set. """
7         pass

    示例:

>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.difference_update(A)
>>> A
{1}

    5)discard——从集合中删除元素

    代码:

1 def discard(self, *args, **kwargs): # real signature unknown
2         """
3         如果一个几个集合中存在这个元素则删除,否则什么都不做
4         Remove an element from a set if it is a member.
5         
6         If the element is not a member, do nothing.
7         """
8         pass

     示例:

>>> A = set([1, 2, 3])  
>>> A.discard(2)
>>> A
{1, 3}
>>> A.discard(4)
>>> A
{1, 3}

     6)intersection——交集

     代码:

1 def intersection(self, *args, **kwargs): # real signature unknown
2         """
3         返回两个集合的交集到一个新的集合中
4         Return the intersection of two sets as a new set.
5         
6         (i.e. all elements that are in both sets.)
7         """
8         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——交集

    代码:

1 def intersection_update(self, *args, **kwargs): # real signature unknown
2         """ 
3         求一个集合与另一个集合的交集,并把结果返回当前集合
4         Update a set with the intersection of itself and another. """
5         pass

    示例:

>>> A = set([1, 2, 3])
>>> B = set([2, 3, 4])
>>> A.intersection_update(B)
>>> A
{2, 3}

    8)isdisjoint——判断是否有交集

    代码:

1 def isdisjoint(self, *args, **kwargs): # real signature unknown
2         """ 
3         判断两个集合是否存在交集,有返回False,没有返回True
4         Return True if two sets have a null intersection. """
5         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——判断当前集合是否是另一个集合的父集合

    代码:

1 def issuperset(self, *args, **kwargs): # real signature unknown
2         """ 
3         判断当前集合是否是另一个集合父集合(另一个集合的所有元素都在当前集合中)
4         Report whether this set contains another set. """
5         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——随机删除元素,并返回删除的元素

    代码:

1 def pop(self, *args, **kwargs): # real signature unknown
2         """
3         随机删除一个元素,并返回那个元素,如果集合为空,将会抛出KeyError异常
4         Remove and return an arbitrary set element.
5         Raises KeyError if the set is empty.
6         """
7         pass

    示例:

>>> s = set([1, 2, 3])
>>> s.pop()           
1

    11)remove——删除一个元素

    代码:

1 def remove(self, *args, **kwargs): # real signature unknown
2         """
3         从集合中删除一个元素,如果要删除的元素不在集合,将会抛出KeyError异常
4         Remove an element from a set; it must be a member.
5         
6         If the element is not a member, raise a KeyError.
7         """
8         pass

    示例:

>>> s = set([1, 2, 3])
>>> s.remove(2)
>>> s
{1, 3}

     12)symmetric_difference——返回两个集合的对称差集

     代码:

1 def symmetric_difference(self, *args, **kwargs): # real signature unknown
2         """
3         返回两个集合的对称差集到一个新的集合
4         Return the symmetric difference of two sets as a new set.
5         
6         (i.e. all elements that are in exactly one of the sets.)
7         """
8         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——当前集合更新为与另一个集合的对称差集

    代码:

1 def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
2         """ 
3         更新当前集合为与另外一个集合的对称差集
4         Update a set with the symmetric difference of itself and another. """
5         pass

    示例:

>>> A = set([1, 2, 3])       
>>> B = set([2, 3, 4, 5])    
>>> A.symmetric_difference_update(B) 
>>> A
{1, 4, 5}

    14)union——返回与另一个集合的并集为一个新的集合

    代码:

1 def union(self, *args, **kwargs): # real signature unknown
2         """
3         返回与另一个集合的并集为一个新的集合
4         Return the union of sets as a new set.
5         
6         (i.e. all elements that are in either set.)
7         """
8         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——更新当前集合为与另一个集合的并集

    代码:

1 def update(self, *args, **kwargs): # real signature unknown
2         """ 
3         更新当前集合为与另一个集合的并集
4         Update a set with the union of itself and others. """
5         pass

    示例:

>>> A = set([1, 2, 3])       
>>> B = set([2, 3, 4, 5])    
>>> A.update(B)
>>> A
{1, 2, 3, 4, 5}
原文地址:https://www.cnblogs.com/zhangxiaxuan/p/5135424.html