最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Python之set集合的相关介绍

    正文概述    2020-04-24   208

    Python之set集合的相关介绍

    认识python中的set集合及其用法

    python中,集合(set)是一个无序排列,可哈希,支持集合关系测试,不支持索引和切片操作,没有特定语法格式,只能通过工厂函数创建.集合里不会出现两个相同的元素,所以集合常用来对字符串或元组或列表中的元素进行去重操作。

    生成一个集合可以使用如下语法:

    生成集合语法1:

    >>> l1=[1,2,3,4,5,6]
    >>> s1=set(l1)
    >>> print(s1)
    {1, 2, 3, 4, 5, 6}

    在这里,使用工厂函数set创建集合,set的参数可以是一个列表,也可以是一个元组或字符串。

    生成集合语法2:

    >>> s2={6,7,8,9,10}
    >>> print(s2)
    {8, 9, 10, 6, 7}

    生成集合语法3:

    >>> s3={i for i in range(10)}
    >>> print(s3)
    {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

    集合类型的方法和操作:

    add

    为集合增加一个元素,如果集合中本来已经存在这个元素对集合无影响
    Add an element to a set.
    This has no effect if the element is already present.
    >>> s1={1,2,3,4,5,6,7}
    >>> s1.add(8)
    >>> print(s1)
    {1, 2, 3, 4, 5, 6, 7, 8}
    >>> s1.add(9)
    >>> print(s1)
    {1, 2, 3, 4, 5, 6, 7, 8, 9}

    clear

    清空集合里所有的元素
    Remove all elements from this set.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9}
    >>> s1.clear()
    >>> print(s1)
    set()
    >>> s2.clear()
    >>> print(s2)
    set()

    copy

    对集合进行浅拷贝(只复制元素,不复制内存地址)
    Return a shallow copy of a set.
    >>> s1={1,2,3,4,5,6,7}
    >>> print(s1,id(s1))
    {1, 2, 3, 4, 5, 6, 7} 140509859430472
    >>> s2=s1.copy()
    >>> print(s2,id(s2))
    {1, 2, 3, 4, 5, 6, 7} 140509842716712

    difference

    求两个或多个集合的差集,并返回一个新集合
    Return the difference of two or more sets as a new set.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s1.difference(s2)
    {1, 2, 3, 4}
    >>> s2.difference(s1)
    {8, 9, 10}

    difference_update

    把两个集合的交集部分从集合中移除
    Remove all elements of another set from this set.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s1.difference_update(s2)
    >>> print(s1)
    {1, 2, 3, 4}
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s2.difference_update(s1)
    >>> print(s2)
    {8, 9, 10}

    相关推荐:《Python视频教程》

    discard

    从集合中移除一个元素,如果被移除的元素不在集合中,不会报错
    Remove an element from a set if it is a member.
    If the element is not a member, do nothing.
    {1, 2, 3, 4, 5, 6, 7}
    >>> s1.discard(7)
    >>> print(s1)
    {1, 2, 3, 4, 5, 6}
    >>> s1.discard(4)
    >>> print(s1)
    {1, 2, 3, 5, 6}
    >>> print(s1)
    {1, 2, 3, 5, 6}

    intersection

    求两个或多个集合中的交集
    Return the intersection of two sets as a new set.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s1.intersection(s2)
    {5, 6, 7}
    >>> s2.intersection(s1)
    {5, 6, 7}

    intersection_update

    把两个集合的交集做为新的集合
    Update a set with the intersection of itself and another.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s1.intersection_update(s2)
    >>> print(s1)
    {5, 6, 7}
    >>> print(s2)
    {5, 6, 7, 8, 9, 10}
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s2.intersection_update(s1)
    >>> print(s2)
    {5, 6, 7}
    >>> print(s1)
    {1, 2, 3, 4, 5, 6, 7}

    isdisjoint

    两个集合没有交集则返回True
    Return True if two sets have a null intersection.
    >>> s1={1,2,3,4,5,6,7}
    >>> s2={5,6,7,8,9,10}
    >>> s1.isdisjoint(s2)
    False
    >>> s1={1,2,3,4}
    >>> s2={6,7,8,9}
    >>> s1.isdisjoint(s2)
    True

    issubset

    如果本集合是参数集合的子集,返回True
    Report whether another set contains this set.
    >>> s1={1,2,3,4}
    >>> s2={1,2,3,4,5,6,7}
    >>> s1.issubset(s2)
    True
    >>> s2.issubset(s1)
    False

    issuperset

    如果本集合是参数集合的超集,返回True
    Report whether this set contains another set.
    >>> s1={1,2,3,4}
    >>> s2={1,2,3,4,5,6,7}
    >>> s1.issuperset(s2)
    False
    >>> s2.issuperset(s1)
    True

    pop

    从集合中移除一个元素,如果集合为空,则报错
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.
    >>> s1={2,3,4,5}
    >>> s1.pop()
    2
    >>> print(s1)
    {3, 4, 5}
    >>> s1.pop()
    3
    >>> s1.pop()
    4
    >>> s1.pop()
    5
    >>> s1.pop()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 'pop from an empty set'

    remove

    移除集合中的一个元素,如果集合是空的,则报错
    Remove an element from a set; it must be a member.  
    If the element is not a member, raise a KeyError.
    >>> s1={1,2,3,4,5,6}
    >>> s1.remove(4)
    >>> print(s1)
    {1, 2, 3, 5, 6}
    >>> s1.remove(9)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    KeyError: 9

    symmetric_difference

    返回两个集合的对称差集的集合
    Return the symmetric difference of two sets as a new set.
    >>> s1={1,2,3,4}
    >>> s2={6,7,8,9}
    >>> s1.symmetric_difference(s2)
    {1, 2, 3, 4, 6, 7, 8, 9}
    >>> s3={1,2,3,4,5,6}
    >>> s4={5,6,7,8,9,10}
    >>> s3.symmetric_difference(s4)
    {1, 2, 3, 4, 7, 8, 9, 10}

    symmetric_difference_update

    与参数集合做对称差集,并返回给自身
    Update a set with the symmetric difference of itself and another.
    >>> s1={1,2,3,4}
    >>> s2={6,7,8,9}
    >>> s2.symmetric_difference_update(s1)
    >>> print(s2)
    {1, 2, 3, 4, 6, 7, 8, 9}
    >>> s3={1,2,3,4,5,6}
    >>> s4={5,6,7,8,9,10}
    >>> s3.symmetric_difference_update(s4)
    >>> print(s3)
    {1, 2, 3, 4, 7, 8, 9, 10}

    union

    求两个或多个集合的并集
    Return the union of sets as a new set.
    >>> s1={1,2,3,4,5,6}
    >>> s2={5,6,7,8,9}
    >>> s1.union(s2)
    {1, 2, 3, 4, 5, 6, 7, 8, 9}
    >>> s3={1,2,3,4}
    >>> s4={6,7,8,9}
    >>> s3.union(s4)
    {1, 2, 3, 4, 6, 7, 8, 9}

    update

    与另一个集合求并集,并返回给自身
    Update a set with the union of itself and others.
    >>> s3={1,2,3,4}
    >>> s4={6,7,8,9}
    >>> s3.update(s4)
    >>> print(s3)
    {1, 2, 3, 4, 6, 7, 8, 9}

    起源地下载网 » Python之set集合的相关介绍

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元