set内置函数梳理

函数名:add

​ Add an element to a set.

  • 将给定的元素添加到集合中。 如果元素已经存在,它不会添加任何元素。

    • 类似list.append()

  >>> s={'1','2','3'}
  
  >>> s.add('4')
  
  >>> s
  
  {'4', '2', '3', '1'}

This has no effect if the element is already present.


函数名:clear
  • 删除集合中的所有元素。

    • list.clear() dict.clear()类似

  
  >>> s
  {'4', '2', '3', '1'}
  >>> s.clear()
  >>> s
  set()

Remove all elements from this set.


函数名:copy
  
  >>> s
  
  {'4', '2', '3', '1'}
  
  >>> s.clear()
  
  >>> s
  
  set()

Return a shallow copy of a set.


函数名:difference
  • 返回两组集合的差异。

  
  >>> s={'1','2','3'}
  
  >>> b={'1','2','4'}
  
  >>> s.difference(b) 
  
  {'3'}
  
  >>> b.difference(s) 
  
  {'4'}

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.)


函数名:difference_update
  • 使用集合的差异更新集合调用difference_update()方法。

  
  >>> s={'1','2','3'}
  
  >>> b={'1','2','4'}
  
  >>> a=b.difference_update(s)
  
  >>> a
  
  >>> s
  
  {'3', '1', '2'}
  
  >>> b
  
  {'4'}
  
  >>> 

Remove all elements of another set from this set.


函数名:discard
  • 从集合中删除指定的元素(如果存在),返回None(表示不存在返回值)。

  
  >>> s={'1','2','3'}
  
  >>> s.discard('3')
  
  >>> s
  
  {'1', '2'}
  
  >>> s.discard('5')

Remove an element from a set if it is a member.

If the element is not a member, do nothing.


函数名:intersection

A.intersection(*other_sets)

  • 返回一个新集合,其中包含所有集合通用的元素。

    • 允许任意数量的参数(集合)。

  
  >>> A = {2, 3, 5, 4}
  
  >>> B = {2, 5, 100}
  
  >>> C = {2, 3, 8, 9, 10}
  
  >>> B.intersection(A)
  
  {2, 5}
  
  >>> B.intersection(C)
  
  {2}
  
  >>> C.intersection(A, B)
  
  {2}

Return the intersection of two sets as a new set.

(i.e. all elements that are in both sets.)


函数名:intersection_update
  • 集合的交集更新set。

  
  >>> A = {1, 2, 3, 4}
  
  >>> B = {2, 3, 4, 5}
  
  \>>> result = A.intersection_update(B)
  
  >>> A
  
  {2, 3, 4}
  
  >>> B
  
  {2, 3, 4, 5}
  
  >>> result

Update a set with the intersection of itself and another.


函数名:isdisjoint

The isdisjoint() method returns True if two sets are disjoint sets. If not, it returns False.

  • 如果两个集合是不相交集合,则isdisjoint()方法返回True。 如果不是,则返回False。

  
  >>> A = {1, 2, 3, 4}
  
  >>> B = {5, 6, 7}
  
  >>> C = {4, 5, 6}
  
  >>> A.isdisjoint(B)
  
  True
  
  >>> A.isdisjoint(C)
  
  False

Return True if two sets have a null intersection.


函数名:issubset

The issubset() method returns True if all elements of a set are present in another set (passed as an argument). If not, it returns False.

  • 如果集合中的所有元素存在于另一个集合中(作为参数传递),则issubset()方法返回True。 如果不是,则返回False。

  
  >>> A = {1, 2, 3}
  
  >>> B = {1, 2, 3, 4, 5}
  
  >>> A.issubset(B)
  
  True
  
  >>> B.issubset(A)
  
  False

Report whether another set contains this set.


函数名:issuperset
  • b.issuperset(a),判断b是否是a的超集

    • 如果一个集合S2中的每一个元素都在集合S1中,且集合S1中可能包含S2中没有的元素,则集合S1就是S2的一个超集。 S1是S2的超集,则S2是S1的真子集,反之亦然

  
  >>> A
  
  {'c', 'd', 'a', 2, 'C', 'A', 'B'}
  
  >>> B={'A','B','C'}
  
  >>> A.issuperset(B)
  
  True

Report whether this set contains another set.


函数名:pop
  • 从集合中删除任意元素并返回被删除的元素。类似list.pop()

  
  >>> A
  
  {1, 2, 3}
  
  >>> A.pop()
  
  1

Remove and return an arbitrary set element.

Raises KeyError if the set is empty.


函数名:remove
  • 搜索集合中的给定元素并将其删除。如果传递给remove()方法的元素(参数)不存在,则会引发keyError异常。

  
  >>> A = {1, 2, 3}
  
  >>> A.remove(1)
  
  >>> A.remove(4)
  
  Traceback (most recent call last):
  
    File "<stdin>", line 1, in <module>
  
  KeyError: 4

Remove an element from a set; it must be a member.

If the element is not a member, raise a KeyError.


函数名:symmetric_difference
  • 返回一个新的集合,它是两个集合的对称差异。

  
  >>> A = {'a', 'b', 'c', 'd'}
  
  >>> B = {'c', 'd', 'e' }
  
  >>> A.symmetric_difference(B)
  
  {'a', 'b', 'e'}
  
  >>> B.symmetric_difference(A)
  
  {'a', 'b', 'e'}

Return the symmetric difference of two sets as a new set.

(i.e. all elements that are in exactly one of the sets.)


函数名:symmetric_difference_update
  • 使用集合的对称差异更新集合

  
  >>> A = {'a', 'c', 'd'}
  
  >>> B = {'c', 'd', 'e' }
  
  >>> A.symmetric_difference_update(B)
  
  >>> A
  
  {'a', 'e'}

Update a set with the symmetric difference of itself and another.


函数名:union

  • 返回一个新集合,其中包含所有集合中不同的元素。

  
  >>> A = {'a', 'c', 'd'}
  
  >>> B = {'c', 'd', 2 }
  
  >>> C= {1, 2, 3}
  
  >>> A.union(B)
  
  {'c', 'd', 'a', 2}
  
  >>> B.union(C)
  
  {'c', 'd', 2, 1, 3}
  
  >>> A.union()
  
  {'d', 'c', 'a'}

Return the union of sets as a new set.

(i.e. all elements that are in either set.)


函数名:update
  • 将集合中的元素(作为参数传递)添加到集合.类似list.extend()

    • 将元素拆开

  
  >>> A
  
  {'d', 'c', 'a'}
  
  >>> A.update(B)
  
  >>> A
  
  {'c', 'd', 'a', 2}
  
  >>> A.update('ABC')
  
  >>> A
  
  {'c', 'd', 'a', 2, 'C', 'A', 'B'}

Update a set with the union of itself and others.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值