zoukankan      html  css  js  c++  java
  • 2 -10 集合set

    1 如何找出同时买iphone和iphone8的人?

     

    iphone7 = ['alex','rain','jack','tom']
    iphone8 = ['alex','shanshan','jack','tom']
    both_list = []
    for name in iphone7:
        if name in iphone8:
            both_list.append(name)
    print(both_list)
    #运行结果
    ['alex', 'jack', 'tom']

    2 集合 set

    集合中的元素有三个特征:
    
    1.确定性(元素必须可hash)
    2.互异性(去重)
    3.无序性(集合中的元素没有先后之分),如集合{3,4,5}和{3,5,4}算作同一个集合。

    注意:集合存在的意义就在于去重和关系运算

     

    3 集合方法

    # 查看所有方法
    >>> dir(s)
    ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getatt
    ribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len_
    _', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__'
    , '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', '
    difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset
    ', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
    >>>
    
    # 方法的帮助
    >>> help(s.clear)
    
    Help on built-in function clear:
    
    clear(...) method of builtins.set instance
        Remove all elements from this set.

    (1)创建

    (2)添加

    • 单个元素的增加 : add(),add的作用类似列表中的append
    • 对序列的增加 : update(),而update类似extend方法,update方法可以支持同时传入多个参数:

    (3)删除

    4 集合测试

    >>> iphone7
    {'rain', 'jack', 'alex', 'tom'}
    >>> iphone8
    {'shanshan', 'jack', 'alex', 'tom'}
    
    # 交集
    >>> iphone7.intersection(iphone8)
    {'jack', 'alex', 'tom'}
    >>> iphone7 & iphone8
    {'jack', 'alex', 'tom'}
    >>>
    
    #差集
    >>> iphone7.difference(iphone8)
    {'rain'}
    >>> iphone7 - iphone8
    {'rain'}
    >>>
    >>> iphone8.difference(iphone7)
    {'shanshan'}
    >>> iphone8 - iphone7
    {'shanshan'}
    
    #并集
    >>> iphone7.union(iphone8)
    {'rain', 'tom', 'shanshan', 'jack', 'alex'}
    >>> iphone7 | iphone8
    {'rain', 'tom', 'shanshan', 'jack', 'alex'}
    >>>
    
    # 对称差集
    # 只买了iphone7 或者iphone8 的人
    >>> iphone7.symmetric_difference(iphone8)
    {'shanshan', 'rain'}
    >>> iphone7 ^ iphone8
    {'shanshan', 'rain'}
    def difference_update(self, *args, **kwargs): # real signature unknown
            """ Remove all elements of another set from this set. """
            pass   
    
    def intersection_update(self, *args, **kwargs): # real signature unknown
            """ Update a set with the intersection of itself and another. """
            pass

    5 包含关系

    in,not in:判断某元素是否在集合内
    ==,!=:判断两个集合是否相等

    两个集合之间一般有三种关系,相交、包含、不相交。在Python中分别用下面的方法判断:

        • set.isdisjoint(s):判断两个集合是不是不相交
        • set.issuperset(s):判断集合是不是包含其他集合,等同于a>=b
        • set.issubset(s):判断集合是不是被其他集合包含,等同于a<=b

  • 相关阅读:
    欧拉路问题
    树上依赖背包总结
    树状数组的应用
    KMP
    深探树形dp
    再探树形dp
    日常水题
    深入hash
    同一控制下的企业合并,长期股权投资成本与支付账面之间的差额计入资本公积
    资本公积冲减留存收益
  • 原文地址:https://www.cnblogs.com/Mobai-c/p/10147409.html
Copyright © 2011-2022 走看看