鱼C论坛

 找回密码
 立即注册
查看: 1760|回复: 1

[技术交流] 浅谈python3--set

[复制链接]
发表于 2017-9-19 14:54:08 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Teagle 于 2017-9-19 14:52 编辑

0x01 set与dict
  1. >>>type({1,2,3})
  2. <class 'set'>

  3. >>>type({1:2})
  4. <class 'dict'>
复制代码

0x02 set函数简介
- 如果set函数参数为空,则返回一个空的set对象
- 如果参数是一个可迭代对象时,则返回去重的一个set对象
  1. >>>help(set)
  2. set() -> new empty set object
  3. set(iterable) -> new set object
  4. Build an unordered collection of unique elements.
复制代码
  1. >>>set([1,1,2,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/False A.issubset(B) A∈B(A属于B)
set.issuperset() 一个可迭代对象| True/False A.issuperset(B)A包含B



0x04 set对象方法解析

  1. >>>dir(set)
  2. ['__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的值不改变

  1.     >>>help(set.difference)
  2.     Return the difference of two or more sets as a new set.
  3.     (i.e. all elements that are in this set but not the others)
复制代码

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.difference({8,9,10})
  4.     {5, 6, 7}
  5.     >>> a
  6.     {5, 6, 7, 8, 9}
复制代码


- set.difference_update(*another_interable)
    - 在上面函数的基础上更新set的值为上面函数的返回值
    - 无返回值
  
  1. >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.difference_update({8,9,10})
  4.     >>> a
  5.     {5, 6, 7}
复制代码



    difference 与 difference_update的区别:
   
    difference是先copy该对象,然后在copy的对象上操作,不影响原对象
   
    difference是在原对象上直接update


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

  1.     >>>help(set.discard)
  2.     Remove an element from a set if it is a member.
  3.     If the element is not a member, do nothind.
复制代码

  1.     >>>a = set(range(10))
  2.     >>>a
  3.     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  4.     >>>a.discard(3)
  5.     >>>a
  6.     {0, 1, 2, 4, 5, 6, 7, 8, 9}
  7.     >>>a.discard(10)#集合内不存在该元素时,不做任何事情
  8.     >>>a
  9.     {0, 1, 2, 4, 5, 6, 7, 8, 9}
复制代码


    ---
    discard与remove的区别:

  1.     >>>help(set.remove)
  2.     Remove an element from a set; it must be a member.
  3.     If the element is not a member, raise a KeyError.
复制代码


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

    ---
3. intersection & intersection_update
- set.intersection(another_iterable) -> set
    - 参数填写可迭代对象
    - 类似数学中的 A∩B,返回一个新的set对象,不影响原set对象的数值
  1.     >>>help(set.intersection)
  2.     Return the intersection of two sets as a new set.
  3.     (i.e. all elements that are in both sets.)
复制代码

  1.     >>> a = set(range(10))
  2.     >>> a
  3.     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  4.     >>> a.intersection([1,3])
  5.     {1, 3}
  6.     >>>
复制代码


- set.intersection_update(another_iterable)
    - 改变set对象的数值为 A ∩ B 的值
    - 无返回值

  1. >>> b
  2.     [5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
  3.     >>> a
  4.     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  5.     >>> a.intersection_update(b)
  6.     >>> a
  7.     {5, 6, 7, 8, 9}
  8.     >>> b
  9.     [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中

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.issubset({3,4})
  4.     False
  5.     >>> a.issubset({4,5,6,7,8,9})
  6.     True
复制代码


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

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.issuperset({5,6})
  4.     True
  5.     >>> a.issuperset({1,2,5,6})
  6.     False
  7.     >>>
复制代码


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

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.union({3,2},{2,4})
  4.     {2, 3, 4, 5, 6, 7, 8, 9}
  5.     >>>a
  6.     >>>{5, 6, 7, 8, 9}
复制代码


- set.update(*other_iterable)
    - 将set的值,更新为set.union(*other_iterable)所返回的

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.union({3,2},{2,4})
  4.     {2, 3, 4, 5, 6, 7, 8, 9}
  5.     >>>a
  6.     {2, 3, 4, 5, 6, 7, 8, 9}
复制代码


6. symmetric_difference & symmetric_difference_update

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

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.symmetric_difference({7,8,9,10,11})
  4.     {5, 6, 10, 11}
  5.     >>> a
  6.     {5, 6, 7, 8, 9}
复制代码

- set.symmeteric_difference_update(another_iterable)
    - 更新set值,更新为set.symmetric_differnece(another_iterable)所返回的

  1.     >>> a
  2.     {5, 6, 7, 8, 9}
  3.     >>> a.symmetric_difference({7,8,9,10,11})
  4.     {5, 6, 10, 11}
  5.     >>>a
  6.     {5, 6, 10, 11}
复制代码



想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-11-6 22:58:30 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 04:09

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表