集合set及其常用方法

Python 集合

集合: 元素唯一性, 无序性.即其不存在索引,元素不重复. 集合是unhashable的.

集合的一些运算符.

  • &: 交集.
  • |: 并集.
  • -: 差集.
  • <: 包含关系.
  • >: 包含关系.
  • ^: 与非集.(并集减去交集的结果)

集合的一些方法,更具体的解释和方法可使用help(obj),dir(obj)或者官方文档查看.

  • add(): 添加一个元素到集合中.
  • clear(): 清空集合内容.
  • copy(): 将一个集合的内容复制到集合中(浅).
  • pop(): 随机弹出一个元素,并返回该元素的值.
  • update(): 添加一个集合对象到集合中.
  • remove(): 选择移除一个元素.
  • isdisjoint(): 与目标对象(list,tuple,set)比较是否相交,相交就返回False,不相交(disjoint)就返回True.
  • issubset(): 与目标对象比较是否为其子集(subset),如果是就返回True,不是就返回False.
  • issuperset(): 与目标对象比较是否为其父集(superset),如果是就返回True,不是就返回False.
>>> a = {1,2,3,4,5}
>>> b = {4,5,6,7}
>>> a
{1, 2, 3, 4, 5}
>>> b
{4, 5, 6, 7}
>>> dir(a)
['__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']
>>> a $ b
SyntaxError: invalid syntax
>>> a & b
{4, 5}
>>> a | b
{1, 2, 3, 4, 5, 6, 7}
>>> a - b
{1, 2, 3}
>>> a ^ b
{1, 2, 3, 6, 7}
>>> a > b
False
>>> a < b
False
>>> a = set()
>>> b = set()
>>> s
Traceback (most recent call last):
  File "<pyshell#14>", line 1, in <module>
    s
NameError: name 's' is not defined
>>> a
set()
>>> b
set()
>>> a = {,}
SyntaxError: invalid syntax
>>> a = { ,}
SyntaxError: invalid syntax
>>> c = {,}
SyntaxError: invalid syntax
>>> c = {, }
SyntaxError: invalid syntax
>>> c
Traceback (most recent call last):
  File "<pyshell#21>", line 1, in <module>
    c
NameError: name 'c' is not defined
>>> a.add(1)
>>> a
{1}
>>> a.clear()
>>> a
set()
>>> b.update((1,2,3))
>>> b
{1, 2, 3}
>>> a.update('123')
>>> a
{'2', '1', '3'}
>>> b.update([4,5,6])
>>> b
{1, 2, 3, 4, 5, 6}
>>> a.update({1,2,3})
>>> a
{1, 2, 3, '1', '2', '3'}
>>> a.remove(5)
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    a.remove(5)
KeyError: 5
>>> a.remove('3')
>>> a
{1, 2, 3, '1', '2'}
>>> a.pop()
1
>>> a
{2, 3, '1', '2'}
>>> a.remove('1','2')
Traceback (most recent call last):
  File "<pyshell#39>", line 1, in <module>
    a.remove('1','2')
TypeError: remove() takes exactly one argument (2 given)
>>> a.remove('1')
>>> a.remove('2')
>>> a
{2, 3}
>>> a.isdisjoint(b)
False
>>> a.issubset(b)
True
>>> a.issuper(b)
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    a.issuper(b)
AttributeError: 'set' object has no attribute 'issuper'
>>> a.issuperset(b)
False
>>> 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值