• 集合


    2、集合类型

             数学上,把set称做由不同的元素组成的集合,集合(set)的成员通常被称做集合元素(set elements)。

            Python把这个概念引入到它的集合类型对象里。Python中集合(sets)有两种不同的类型:可变集合(set)和不可变集合(frozenset)。对可变集合(set),可以添加和删除元素,对不可变集合(frozenset)则不允许这样做。

    注意:集合类型是无序,不可重复的。可变集合(set)不是可哈希的,因此既不能用做字典的键也不能做其他集合中的元素。不可变集合(frozenset)则正好相反,即它们有哈希值,能被用做字典的键或是作为集合中的一个成员。

    (1)集合类型的创建

           集合类型是无序,不可重复的。 set()和 frozenset()工厂函数分别用来生成可变和不可变的集合。如果不提供任何参数,默认会生成空集合。如果提供一个参数,则该参数必须是可迭代的,如:set(iterable) -> new set object 其中iterable可以是String、List、Tuple、Dictionary。但是为dict时,只会获取提Key作为set的元素。

    >>> s = set()
    >>> type(s)
    <class 'set'>
    >>> a = set('python')
    >>> a
    {'y', 'o', 't', 'n', 'p', 'h'}
    >>> b = set([2,4,3,7])
    >>> b
    {2, 3, 4, 7}
    >>> c= frozenset({1:'p',2:'y',3:'t'})
    >>> c
    frozenset({1, 2, 3})
    >>> a = set([1,2,1,3])
    >>> a
    {1, 2, 3}
    >>> b = frozenset([1,2,1,3])
    >>> b
    frozenset({1, 2, 3})
    >>>

    (2)集合类型操作符

     

    obj in s

    成员测试:obj 是 s 中的一个元素吗?

    obj not in s

    非成员测试:obj 不是 s 中的一个元素吗?

    s == t

    等价测试: 测试 s 和 t 是否具有相同的元素?

    s != t

    不等价测试: 与==相反

    s < t

    (严格意义上)子集测试; s != t 而且 s 中 所 有 的元素都是 t 的成员

    s <= t

    子集测试(允许不严格意义上的子集): s 中所有的元素都是 t 的成员

    s > t

    (严格意义上)超集测试: s != t 而且 t 中所有的元素都是 s 的成员

    s >= t

    超集测试(允许不严格意义上的超集): t 中所有的元素 都是 s 的成员

    s | t

    合并操作: s 或 t 中的元素

     s & t

    交集操作: s 和 t 中的元素

     s - t

    差分操作: s 中的元素,而不是 t 中的元素

    s ^ t

     对称差分操作:s 或 t 中的元素,但不是 s 和 t 共有的元素

    (3)集合内置方法

    len(s)

    集合基数: 集合 s 中元素的个数

    set([obj])

    可变集合工厂函数; obj 必须是支持迭代的,由 obj 中的元素创建集合,否则创建一个空集合

    frozenset([obj])

    不可变集合工厂函数; 执行方式和 set()方法相同,但它返回的是不可变集合

    s.issubset(t)

    即s <= t, 子集测试(允许不严格意义上的子集): s 中所有的元素都是 t 的成员

    s.issuperset(t)

    即s >= t ,超集测试(允许不严格意义上的超集): t 中所有的元素 都是 s 的成员

    s.isdisjoint(t)

    判断是否有交集, 如果没有交集,返回True,否则返回False

    s.union(t)

    即s | t 合并操作: s 或 t 中的元素

    s.intersection(t)

    即s & t 交集操作: s 和 t 中的元素

    s.difference(t)

    即s - t 差分操作: s 中的元素,而不是 t 中的元素

    s.symmetric_difference(t)

    即s ^ t 对称差分操作:s 或 t 中的元素,但不是 s 和 t 共有的元素

    s.copy()

    复制操作:返回 s 的(浅复制)副本

    (4)set(可变集合)的内置方法

    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. A中存在,B中不存在
    
            (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.  从当前集合中删除和B中相同的元素"""
    
            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.  取交集并更更新到A中 """
    
            pass
    
    
        def isdisjoint(self, *args, **kwargs): # real signature unknown
    
            """ Return True if two sets have a null intersection.  如果没有交集,返回True,否则返回False"""
    
            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. 对称交集,并更新到a中 """
    
            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
    View Code

    s.add(obj)

     加操作: 将 obj 添加到 s

    s.update(t)

    s |= t (Union) 修改操作: 将 t 中的成员添加 s

    s.intersection_update(t)

    s &= t 交集修改操作: s 中仅包括 s 和 t 中共有的成员

    s.difference_update(t)

     s -= t 差修改操作: s 中包括仅属于 s 但不属于 t 的成员

    s.symmetric_difference_update(t)

     s ^= t 对称差分修改操作: s 中包括仅属于 s 或仅属于 t 的成员

    s.remove(obj)

     删除操作: 将 obj 从 s 中删除;如果 s 中不存在obj,将引发 KeyError

    s.discard(obj)

     丢弃操作: remove() 的 友 好 版 本 - 如果 s 中存在 obj,从 s 中删除它

    s.pop()

     Pop 操作: 移除并返回 s 中的任意一个元素

    s.clear()

    清除操作: 移除 s 中的所有元素

  • 相关阅读:
    python 进程
    python 网络编程
    Tensorflow学习教程------参数保存和提取重利用
    Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_利用训练好的模型进行分类
    Tensorflow学习教程------利用卷积神经网络对mnist数据集进行分类_训练模型
    Tensorflow学习教程------tensorboard网络运行和可视化
    Tensorflow学习教程------过拟合
    Spring的@Value注解为静态变量赋值
    Linux使用scp远程拷贝使用ssh免密登录
    Mysql升级5.7.10后GROUP BY语句出错解决方法
  • 原文地址:https://www.cnblogs.com/kuenen/p/5963083.html
Copyright © 2020-2023  润新知