zoukankan      html  css  js  c++  java
  • python学习笔记_集合的定义和常用方法

    1、认识集合

    定义:

    s={1,2,3,4,5}

    s=set("hello")

    s=set(["steven","job","dave"])

     用set和可迭代对象进行定义时,相当于执行了for循环

    补充:集合属于可变类型,如果定义不可变类型的集合用frozenset(),

    如s=frozenset("hello")

    print(s)  #结果为:frozenset({'h', 'o', 'l', 'e'})

    特点:

    不同元素组成

    无序

    集合中的元素必须是不可变  (字符串,数字,元祖)

    2、常用方法

    .add("a")    #增加一个元素

    .clear()      #清空

    .copy()      #浅拷贝

    .pop()       #随机删

    .remove("job")       #指定删,删除元素不存在时会报错

    .discard("job")       #指定删,删除元素不存在时不会报错

    3、基本运算

    交集,并集,差集

    a_s={"steve","job","dave","max"}
    b_s={"job","dave","mark"}
    print(a_s,b_s)
    
    #求交集
    print(a_s.intersection(b_s))        #写法1
    print(a_s&b_s)                      #写法2
    
    #求并集
    print(a_s.union(b_s))               #写法1
    print(a_s|b_s)                      #写法2
    
    #求差集,相当于存在于左边,但不存在于右边的集合
    print(a_s.difference(b_s))          #写法1
    print(a_s-b_s)                      #写法2
    print(b_s-a_s)

    结果:

    {'dave', 'steve', 'max', 'job'} {'dave', 'mark', 'job'}
    {'dave', 'job'}
    {'dave', 'job'}
    {'job', 'dave', 'mark', 'steve', 'max'}
    {'job', 'dave', 'mark', 'steve', 'max'}
    {'steve', 'max'}
    {'steve', 'max'}
    {'mark'}
    

     4、其他方法

    a_s={"steve","job","dave","max"}
    b_s={"job","dave","mark"}
    c_s={"job","dave"}
    
    #交叉补集 ,交集的补集,相当于并集减去交集的部分的集合
    print(a_s.symmetric_difference(b_s))    #写法1
    print(a_s^b_s)                          #写法2
    #结果为:{'mark', 'max', 'steve'}
    
    #.difference_update()
    a_s.difference_update(b_s)    # 相当于 a_s=a_s-b_s
    print(a_s)
    #结果为:{'max', 'steve'}
    
    #.isdisjoint()  判断有没有交集,没有交集返回true
    print(a_s.isdisjoint(b_s))
    #结果为:false
    
    #.issubset()  是否是某个集合的子集。相当于是s1<=s2
    print(c_s.issubset(a_s))      #写法1
    print(c_s<=a_s)               #写法2
    #.issuperset() 是否是某个集合的父集。相当于是s1>=s2
    print(a_s.issuperset(c_s))
    #结果为 true
    
    #.update() 可以增加多个值。可以传元祖,列表等可迭代对象
    a_s.update(b_s)
    print(a_s)
    b_s.update(("bob","zhou"))
    print(b_s)
    #结果为:
    #{'steve', 'job', 'max', 'dave', 'mark'}
    #{'bob', 'mark', 'job', 'dave', 'zhou'}
    """
        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. """
            passseset
    set
  • 相关阅读:
    传统文化相关词组(陆续补充)
    面试题 17.09. 第 k 个数
    1544. 整理字符串
    SQL Server 2008 R2 数据库之间的数据同步热备份
    SQLServer数据库同步准实时解决方案
    SQL Server 用链接服务器 同步MySQL
    SqlServer数据库同步方案详解(包括跨网段)
    键值修饰符v-on:keyup.enter
    事件修饰符
    内连处理器里的方法2.html
  • 原文地址:https://www.cnblogs.com/steven223-z/p/12149621.html
Copyright © 2011-2022 走看看