一、集合基础

1、创建集合

使用set类创建集合

在使用set类创建集合是=时,需要为set类的构造方法提供一个列表或者元组类型的值,用于建立集合的数据源;这也就是说set类可以将元组或列表转为集合,并且去除重复元素,元素顺序可能也会被打乱,因为集合是无序的。

#利用列表创建集合
s=set([1,2,3])
print(s) #{1, 2, 3}
#利用元组创建集合
 s=set((1,2,3)) 
 print(s) #{1, 2, 3}

使用{}直接创建

s={1,2,3}
print(type(s))#<class 'set'>

2、集合特性

(1)无序性

集合中的值是平等的,元素之间是无序的,无法通过索引和分片进行操作。

(2)互异性

集合中任意两个元素之间是不同的,即每个元素只能出现一次,常常用于去重应用。

(3)确定性

集合内的元素是不可变数据类型,例如,集合、列表、字典都不能作为集合的元素,因为它们都是可变的。
3、元素检测

判断一个元素是否属于该集合,使用in,如果在就返回True,如果不在就返回False。

s=set("hello")
print("h" in s)#True

4、集合遍历

######遍历字符串#####
s=set("hello")
for i in s:
    print(i)
#########输出#####
        o
        e
        l
        h
#######遍历元组####
 s={(1,2),(3,4),(5,6)}
   for item in s: 
      print(item)
#########输出########## 
     (5, 6) 
     (3, 4)      
     (1, 2)
二、集合方法

set类

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
class set(object):
    """
    set() -> new empty set object
    set(iterable) -> new set object
    
    Build an unordered collection of unique elements.
    """
    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

    def clear(self, *args, **kwargs): # real signature unknown
        """ Remove all elements from this set. """
        pass

    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a set. """
        pass

    def difference(self, *args, **kwargs): # real signature unknown
        """
        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

    def difference_update(self, *args, **kwargs): # real signature unknown
        """ Remove all elements of another set from this set. """
        pass

    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

    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

    def intersection_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the intersection of itself and another. """
        pass

    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection. """
        pass

    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set. """
        pass

    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. """
        pass

    def pop(self, *args, **kwargs): # real signature unknown
        """
        Remove and return an arbitrary set element.
        Raises KeyError if the set is empty.
        """
        pass

    def remove(self, *args, **kwargs): # real signature unknown
        """
        Remove an element from a set; it must be a member.
        
        If the element is not a member, raise a KeyError.
        """
        pass

    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

    def symmetric_difference_update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the symmetric difference of itself and another. """
        pass

    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

    def update(self, *args, **kwargs): # real signature unknown
        """ Update a set with the union of itself and others. """
        pass

    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __iand__(self, *args, **kwargs): # real signature unknown
        """ Return self&=value. """
        pass

    def __init__(self, seq=()): # known special case of set.__init__
        """
        set() -> new empty set object
        set(iterable) -> new set object
        
        Build an unordered collection of unique elements.
        # (copied from class doc)
        """
        pass

    def __ior__(self, *args, **kwargs): # real signature unknown
        """ Return self|=value. """
        pass

    def __isub__(self, *args, **kwargs): # real signature unknown
        """ Return self-=value. """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __ixor__(self, *args, **kwargs): # real signature unknown
        """ Return self^=value. """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass

    def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass

    def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass

    def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass

    def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass

    __hash__ = None
  • add 向集合中添加一个元素
  • clear 从集合中移除所有元素
  • copy 返回集合的浅拷贝
  • difference 将两个或多个集合的差集作为一个新集合返回
  • difference_update 从这个集合中删除另一个集合的所有元素
  • discard 移除一个已经存在于集合中的元素(如果元素不存在,则不执行任何操作)
  • intersection 将两个集合的交集作为一个新集合返回
  • intersection_update 自身集合和另一个的交集来更新这个集合
  • isdisjoint 如果两个集合有一个空交集,返回 True
  • issubset 如果另一个集合包含这个集合,返回 True
  • issuperset 如果这个集合包含另一个集合,返回 True
  • pop 删除并返回任意的集合元素(如果集合为空,会抛出 KeyError异常)
  • remove 删除集合中的一个元素(如果元素不存在,会抛出KeyError异常)
  • symmetric_difference 将两个集合的对称差作为一个新集合返回(两个集合合并删除相同部分,其余保留)
  • symmetric_difference_update 用自己和另一个的对称差来更新这个集合
  • union 将集合的并集作为一个新集合返回
  • update 用自己和另一个的并集来更新这个集合

1、add()

s={1,2,4,}
s.add(5)
print(s)#{1, 2, 4, 5}

2、clear()

s={'hell0','world'}
s.clear()
print(s)#set()

3、copy ()

s1={1,2,3,}
s2=s1.copy()
print(s2)#{1, 2, 3}

4、difference()

s1={'a','b','c'}
s2={'a','d','c','e'}
#求出s1和s2之间的差集,保留s1中不同的元素
print(s1.difference(s2))#{'b'}
print(s1-s2)#{'b'}

#保留s2中相同的元素
print(s2.difference(s1))#{'d', 'e'}
print(s2-s1)#{'d', 'e'}

5、difference_update()

s1={1,2,4,5,}
s2={2,4,6,8}
s3={6}
s1.difference_update(s2)
#从s1中删除存在s2的元素
print(s1)#{1, 5}

#从s2中删除存在s3的元素
s2.difference_update(s3)
print(s2)#{8, 2, 4}

6、discard()

s={"a","b","c"}
s.discard("b")
#从s中移除元素b
print(s)#{'a', 'c'}

7、intersection()

s1={1,2,4,5,}
s2={2,4,6,8}
#将s1和s2求交集并且返回新的集合
print(s1.intersection(s2))#{2, 4}
print(s1&s2)#{2, 4}

8、intersection_update()

s1={'a','b','c'}
s2={'a','d','c','e'}
#相当于s1-s2,这与差集difference差不多
s1.intersection_update(s2)
print(s1)#{'a', 'c'}

9、isdisjoint()

s1={'a','b','c'}
s2={'a','d','c','e'}
#判断s1与s2之间是否有空交集,如果没有返回False
print(s1.isdisjoint(s2))#False

10、issubset()

s1={1,2,3}
s2={2}
#因为s1集合包含s2,所以返回True
print(s2.issubset(s1))#True

11、issuperset()

s1={1,2,3}
s2={2}
#因为s1集合包含s2,所以返回True
print(s1.issuperset(s2))#True

它与issubset的区别在于,包含的集合作为方法的调用者,被包含集合作为方法的参数。

12、pop()

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:778463939
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
s={1,3,5,2,8,3,9}
#随机删除元素并且返回删除元素
print(s.pop())#1
print(s)#{2, 3, 5, 8, 9}

13、remove()

s={1,3,5,2,8,3,9}
#删除指定元素并且无返回元素
print(s.remove(5))#None
print(s)#{1, 2, 3, 8, 9}

14、symmetric_difference()

s1={'a','b','c'}
s2={'a','d','c','e'}
#s1与s2中删除相同元素,保留不同元素,求对称差,相当于s1-s2与s2-s1的并集
print(s1.symmetric_difference(s2))#{'d', 'b', 'e'}

15、symmetric_difference_update()

s1={1,2,3,4,5}
s2={2,3,4}
s3={1,}
#将s1与s2集合的对称集求出来,然后与s1集合进行更新
s1.symmetric_difference_update(s2)
print(s1)#{1, 5}
#将s3与s2集合的对称集求出来,然后与s3集合进行更新
s3.symmetric_difference_update(s2)
print(s3)#{1, 2, 3, 4}

16、union()

s1={1,2,3,4,5}
s2={2,3,4}
#求出s1与s2的并集
print(s1.union(s2))#{1, 2, 3, 4, 5}
print(s1|s2)#{1, 2, 3, 4, 5}

17、update()

s1={1,2,3,4,5}
s2={2,3,4}
#对s1进行更新,相当于求两个集合的并集
s1.update(s2)
print(s1)#{1, 2, 3, 4, 5}
三、集合常用方法

1、并集

使用union方法或者操作符“|”进行两个或者多个集合求并集

namelist1={'alia','sanb','lige'}
namelist2={'alia','bobu'}
print(namelist1|namelist2)#{'alia', 'sanb', 'bobu', 'lige'}
print(namelist1.union(namelist2))#{'alia', 'sanb', 'bobu', 'lige'}

2、交集

使用intersection方法或者操作符“&”进行两个或多个集合求交集

namelist1={'alia','sanb','lige'}
namelist2={'alia','bobu'}
print(namelist1&namelist2)#{'alia'}
print(namelist1.intersection(namelist2))#{'alia'}

3、差集

使用difference方法或者操作符“-”进行两个或多个集合求差集

namelist1={'alia','sanb','lige'}
namelist2={'alia','bobu'}
print(namelist1-namelist2)#{'sanb', 'lige'}
print(namelist1.difference(namelist2))#{'sanb', 'lige'}

4、对称差集

使用symmetric_difference方法或者操作符“^”进行两个或多个集合求对称差集

namelist1={'alia','sanb','lige'}
namelist2={'alia','bobu'}
print(namelist1^namelist2)#{'sanb', 'bobu', 'lige'}
print(namelist1.symmetric_difference(namelist2))#{'sanb', 'bobu', 'lige'}

5、子集

使用issubset方法或者操作符“<”判断当前集合是否是某个集合的子集

str1=set("hello")
str2=set("he")
#判断str2是否是str1的子集
print(str2<str1)#True
print(str2.issubset(str1))#True
四、集合与函数

1、len()

获取集合中元素的个数

s={1,2,4,5,3,8}
print(len(s))#6

2、max()

获取集合中最大的元素

s={1,2,4,5,3,8}
print(max(s))#8

3、min()

获取集合中最小的元素

s={1,2,4,5,3,8}
print(min(s))#1

4、sum()

返回集合的所有元素之和

s={1,2,4,5,3,8}
print(sum(s))#23

5、sorted()

返回排序后的集合列表

s={1,2,4,5,3,8}
print(sorted(s))#[1, 2, 3, 4, 5, 8]

6、enumerate()

返回一个可迭代的enumerate的数据类型,迭代后可以取出索引值及其具体的值。

s={1,2,4,5,3,8}
print(enumerate(s))#<enumerate object at 0x00000000054DA558>
for i,j in enumerate(s):
    print(i,j)

#######输出#######
    <enumerate object at 0x00000000054DA558>
    0 1
    1 2
    2 3
    3 4
    4 5
    5 8

7、all()

如果集合中的所有元素都是 True(或者集合为空),则返回 True。

s={0,1,2}
print(all(s))#False

#集合为空
s={}
print(all(s))#True

8、any()

如果集合中的所有元素都是 True,则返回 True;如果集合为空,则返回 False。

s={0,1,2}
print(any(s))#True

#集合为空
s={}
print(any(s))#False
五、frozenset只读集合

frozenset类

class frozenset(object):
    """
    frozenset() -> empty frozenset object
    frozenset(iterable) -> frozenset object
    
    Build an immutable unordered collection of unique elements.
    """
    def copy(self, *args, **kwargs): # real signature unknown
        """ Return a shallow copy of a set. """
        pass

    def difference(self, *args, **kwargs): # real signature unknown
        """
        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

    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

    def isdisjoint(self, *args, **kwargs): # real signature unknown
        """ Return True if two sets have a null intersection. """
        pass

    def issubset(self, *args, **kwargs): # real signature unknown
        """ Report whether another set contains this set. """
        pass

    def issuperset(self, *args, **kwargs): # real signature unknown
        """ Report whether this set contains another set. """
        pass

    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

    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

    def __and__(self, *args, **kwargs): # real signature unknown
        """ Return self&value. """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x. """
        pass

    def __eq__(self, *args, **kwargs): # real signature unknown
        """ Return self==value. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __ge__(self, *args, **kwargs): # real signature unknown
        """ Return self>=value. """
        pass

    def __gt__(self, *args, **kwargs): # real signature unknown
        """ Return self>value. """
        pass

    def __hash__(self, *args, **kwargs): # real signature unknown
        """ Return hash(self). """
        pass

    def __init__(self, seq=()): # known special case of frozenset.__init__
        """ Initialize self.  See help(type(self)) for accurate signature. """
        pass

    def __iter__(self, *args, **kwargs): # real signature unknown
        """ Implement iter(self). """
        pass

    def __len__(self, *args, **kwargs): # real signature unknown
        """ Return len(self). """
        pass

    def __le__(self, *args, **kwargs): # real signature unknown
        """ Return self<=value. """
        pass

    def __lt__(self, *args, **kwargs): # real signature unknown
        """ Return self<value. """
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __ne__(self, *args, **kwargs): # real signature unknown
        """ Return self!=value. """
        pass

    def __or__(self, *args, **kwargs): # real signature unknown
        """ Return self|value. """
        pass

    def __rand__(self, *args, **kwargs): # real signature unknown
        """ Return value&self. """
        pass

    def __reduce__(self, *args, **kwargs): # real signature unknown
        """ Return state information for pickling. """
        pass

    def __repr__(self, *args, **kwargs): # real signature unknown
        """ Return repr(self). """
        pass

    def __ror__(self, *args, **kwargs): # real signature unknown
        """ Return value|self. """
        pass

    def __rsub__(self, *args, **kwargs): # real signature unknown
        """ Return value-self. """
        pass

    def __rxor__(self, *args, **kwargs): # real signature unknown
        """ Return value^self. """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __sub__(self, *args, **kwargs): # real signature unknown
        """ Return self-value. """
        pass

    def __xor__(self, *args, **kwargs): # real signature unknown
        """ Return self^value. """
        pass

由于集合是可变的,所以不能作为集合的元素或者字典的key,但是可以利用frozenset类型将集合转为可读的,这样就可以作为集合的元素以及字典的key。

定义两个集合s1,s2
s1=set("abcdefg")
s2=set([1,2,3,4])
#向s1,s2中添加元素
s1.add("h")
s2.add(5)
print(s1)
print(s2)
#############输出##########
    {'a', 'd', 'c', 'f', 'e', 'h', 'g', 'b'}
    {1, 2, 3, 4, 5}

上面是正常的添加过程,但是s1集合添加s2,就会抛出异常

#定义两个集合s1,s2
s1=set("abcdefg")
s2=set([1,2,3,4])
s1.add(s2)
print(s1)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-120-33029c9f337b> in <module>
      2 s1=set("abcdefg")
      3 s2=set([1,2,3,4])
----> 4 s1.add(s2)
      5 print(s1)

TypeError: unhashable type: 'set'

这时使用frozenset函数将s2集合转为可读集合,再进行添加

#定义两个集合s1,s2
s1=set("abcdefg")
s2=set([1,2,3,4])

s1.add(frozenset(s2))
print(s1)

###########输出######
{'a', 'd', 'c', frozenset({1, 2, 3, 4}), 'f', 'e', 'g', 'b'}

同理这时frozenset后的集合也可以作为字典的key。

dict={"name":"bright"}
s2=set([1,2,3,4])

dict.setdefault(frozenset(s2),[7,8,9])
print(dict)
##########输出#########
{frozenset({1, 2, 3, 4}): [7, 8, 9], 'name': 'bright'}