浅谈python3-set

0x01 set与dict

>>>type({1,2,3})
<class 'set'>

>>>type({1:2})
<class 'dict'>

0x02 set函数简介

  • 如果set函数参数为空,则返回一个空的set对象
  • 如果参数是一个可迭代对象时,则返回去重的一个set对象
>>>help(set)
set() -> new empty set object
set(iterable) -> new set object
Build an unordered collection of unique elements.
>>>set([1,1,2,2])
{1,2}

0x03 set对象方法图示

方法参数返回值举例数学
set.intersection()一个可迭代对象set对象A.intersection(B)A∩B
set.union()一个或多个可迭代对象set对象A.intersection(B,C)A∪B∪C
set.difference()一个或多个可迭代对象set对象A.difference(B,C)A-A∩B-A∩C
set.discard()一个元素A.difference(a)A-a
set.issubset()一个可迭代对象True/FalseA.issubset(B)A∈B(A属于B)
set.issuperset()一个可迭代对象True/FalseA.issuperset(B)A包含B

0x04 set对象方法解析

>>>dir(set)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__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']

这里笔者只说set对象中独有的,像add(),clear()等方法就不在此赘述了

  1. difference & difference_update

    • set.difference(*another_interable) -> set
    • 类比数学中的 A - A ∩ B,返回set中独有的元素所构成的新的set对象
    • set的值不改变

      >>>help(set.difference)
      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)
      >>> a
      {5, 6, 7, 8, 9}
      >>> a.difference({8,9,10})
      {5, 6, 7}
      >>> a
      {5, 6, 7, 8, 9}
    • set.difference_update(*another_interable)

    • 在上面函数的基础上更新set的值为上面函数的返回值
    • 无返回值
      >>> a
      {5, 6, 7, 8, 9}
      >>> a.difference_update({8,9,10})
      >>> a
      {5, 6, 7}
      

    difference 与 difference_update的区别:

    difference是先copy该对象,然后在copy的对象上操作,不影响原对象

    difference是在原对象上直接update


  2. discard && remove

    • set.discard(element)
    • 从set对象中移除指定元素,如果该元素不在该set对象中,则什么都不做

      >>>help(set.discard)
      Remove an element from a set if it is a member.
      If the element is not a member, do nothind.
      >>>a = set(range(10))
      >>>a
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      >>>a.discard(3)
      >>>a
      {0, 1, 2, 4, 5, 6, 7, 8, 9}
      >>>a.discard(10)#集合内不存在该元素时,不做任何事情
      >>>a
      {0, 1, 2, 4, 5, 6, 7, 8, 9}

    discard与remove的区别:

    >>>help(set.remove)
    Remove an element from a set; it must be a member.
    If the element is not a member, raise a KeyError.

    如果集合内不存在该元素,抛出KeyError异常


  3. intersection & intersection_update

    • set.intersection(another_iterable) -> set
    • 参数填写可迭代对象
    • 类似数学中的 A∩B,返回一个新的set对象,不影响原set对象的数值

      >>>help(set.intersection)
      Return the intersection of two sets as a new set.
      (i.e. all elements that are in both sets.)
      >>> a = set(range(10))
      >>> a
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      >>> a.intersection([1,3])
      {1, 3}
      >>>
    • set.intersection_update(another_iterable)

    • 改变set对象的数值为 A ∩ B 的值
    • 无返回值
      >>> b
      [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
      >>> a
      {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
      >>> a.intersection_update(b)
      >>> a
      {5, 6, 7, 8, 9}
      >>> b
      [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
      >>>
  4. issubset & issuperset

    • set.issubset(another_iterable)
    • 判断set是否属于another_iterable,属于则返回True,不属于则返回False
    • 即判断set里的元素是否都在another_iterable中

      >>> a
      {5, 6, 7, 8, 9}
      >>> a.issubset({3,4})
      False
      >>> a.issubset({4,5,6,7,8,9})
      True
    • set.issuperset(another_iterable)

    • 判断another_iterable是否属于set,属于则返回True,不属于则返回False
    • 即判断another_iterable里的元素是否都在set中
      >>> a
      {5, 6, 7, 8, 9}
      >>> a.issuperset({5,6})
      True
      >>> a.issuperset({1,2,5,6})
      False
      >>>
  5. union & update

    • set.union(*other_iterable)
    • 取所有对象的并集,即A∪B∪C…
    • set的值不改变

      >>> a
      {5, 6, 7, 8, 9}
      >>> a.union({3,2},{2,4})
      {2, 3, 4, 5, 6, 7, 8, 9}
      >>>a
      >>>{5, 6, 7, 8, 9}
    • set.update(*other_iterable)

    • 将set的值,更新为set.union(*other_iterable)所返回的
      >>> a
      {5, 6, 7, 8, 9}
      >>> a.union({3,2},{2,4})
      {2, 3, 4, 5, 6, 7, 8, 9}
      >>>a
      {2, 3, 4, 5, 6, 7, 8, 9}
  6. symmetric_difference & symmetric_difference_update

    • set.symmetric_difference(another_iterable)
    • 返回两个对象中非都存在的元素,类似数学中的A∪B-A∩B
    • set的值不改变

      >>> a
      {5, 6, 7, 8, 9}
      >>> a.symmetric_difference({7,8,9,10,11})
      {5, 6, 10, 11}
      >>> a
      {5, 6, 7, 8, 9}
    • set.symmeteric_difference_update(another_iterable)

    • 更新set值,更新为set.symmetric_differnece(another_iterable)所返回的
      >>> a
      {5, 6, 7, 8, 9}
      >>> a.symmetric_difference({7,8,9,10,11})
      {5, 6, 10, 11}
      >>>a
      {5, 6, 10, 11}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值