Python 集合方法总结

1、添加一个元素:
    add(...)

       Addan element to a set.

1
2
3
4
>>> a = { 'shaw' , 11 , 22 }
>>>a.add( 'sina' )
>>> a
{ 'sina' , 11 , 22 , 'shaw' }

2、清空集合

    clear(...)

       Remove all elements from this set.

1
2
3
4
>>> a = { 'shaw' , 11 , 22 }
>>>a.clear()
>>> a
set ()

3、浅copy

    copy(...)

      Return a shallow copy of a set.

1
2
3
4
>>> a = { 'shaw' , 11 , 22 }
>>> b = a.copy()
>>> print (b)
{ 11 , 22 , 'shaw' }

4、返回一个A中存在,B中不存在元素的集合

    difference(...)

      Return the difference of two or more setsas a new set.

1
2
3
4
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 11 , "sam" }
>>>A.difference(B)
{ 22 , 'shaw' }

5、从当前元素中删除和B中相同的元素

    difference_update(...)

      Remove all elements of another set fromthis set.

1
2
3
4
5
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 11 , "sam" }
>>>A.difference_update(B)
>>> print (A)
{ 22 , 'shaw' }

6、删除指定元素,如果不存在不会报错

    discard(...)

      Remove an element from a set if it is amember.

1
2
3
4
5
6
7
>>> A = { 'shaw' , 11 , 22 }
>>>A.discard( 11 )
>>> print (A)
{ 22 , 'shaw' }
>>>A.discard( 15 )
>>> print (A)
{ 22 , 'shaw' }

7、取交集

    intersection(...)

      Return the intersection of two sets as anew set.

1
2
3
4
>>> a = { 'shaw' , 11 , 22 }
>>> b = { 'sam' , 22 }
>>>a.intersection(b)
{ 22 }

8、取交集,并更新到A

    intersection_update(...)

      Update a set with the intersection ofitself and another.

1
2
3
4
5
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 'sam' , 22 }
>>>A.intersection_update(B)
>>> print (A)
{ 22 }

9、判断是否有交集,如果没有交集,返回True,否则返回False

    isdisjoint(...)

      Return True if two sets have a nullintersection.

1
2
3
4
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 'sam' , 22 }
>>>A.isdisjoint(B)
False

10、判断是否是子序列

    issubset(...)

      Report whether another set contains thisset.

1
2
3
4
5
6
7
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 'sam' , 22 }
>>>B.issubset(A)
False
>>> C = { 'shaw' }
>>>C.issubset(A)
True

11、判断是否是父序列

    issuperset(...)

      Report whether this set contains anotherset.

1
2
3
4
>>> A = { 'shaw' , 11 , 22 }
>>> B = { 22 }
>>>A.issuperset(B)
True

12、移除元素,如果集合为空,会报错

    pop(...)

      Remove and return an arbitrary setelement.

      Raises KeyError if the set is empty.

1
2
3
4
5
>>> A = { 'shaw' , 11 , 1 , 98 , 3 , 'abc' , 'Shaw' }
>>>A.pop()
1
>>> print (A)
{ 98 , 3 , 'abc' , 11 , 'Shaw' , 'shaw' }

13、删除指定元素,元素不存在会报错

    remove(...)

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

      If the element is not a member, raise aKeyError.

1
2
3
4
5
6
7
8
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' }
>>>A.remove( '22' )
Traceback (mostrecent call last):
    File "<input>" , line 1 , in <module>
KeyError: '22'
>>>A.remove( 22 )
>>> print (A)
{ 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 'shaw' }

14、取并集

    union(...)

      Return the union of sets as a new set.

1
2
3
4
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' }
>>> B = { 11 , 'sam' }
>>>A.union(B)
{ 'sam' , 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 22 , 'shaw' }

15、对称差集

    symmetric_difference(...)

      Return the symmetric difference of twosets as a new set.

1
2
3
4
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' }
>>> B = { 11 , 'sam' }
>>>A.symmetric_difference(B)
{ 'sam' , 1 , 98 , 3 , 'abc' , 'Shaw' , 22 , 'shaw' }

16、更新

    update(...)

      Update a set with the union of itself andothers.

1
2
3
4
5
>>> A = { 'shaw' , 11 , 22 , 1 , 98 , 3 , 'abc' , 'Shaw' }
>>> B = { 11 , 'sam' }
>>>A.update(B)
>>> print (A)
{ 'sam' , 1 , 98 , 3 , 'abc' , 11 , 'Shaw' , 22 , 'shaw' }

 

转载于:https://www.cnblogs.com/opsedu/p/5546559.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值