python_set

set 的常用函数
创建集合的方法

  • se = {11,22,33} 该方法不能用于创建空集合
  • se = set() 该方法可以创建空集合

set 的常用函数

def add(self, *args, **kwargs)
Add an element to a set.
        This has no effect if the element is already present
向集合中添加一个元素,如果该元素存在也不影响
def clear(self, *args, **kwargs)
Remove all elements from this set
清空该集合中的所有元素
def difference(self, *args, **kwargs)
Return the difference of two or more sets as a new set.
返回一个新的集合,用来存储两个set的不同   
a = {1, 2, 3, 4, 5}
b = {2, 4, 5}

d = a.difference(b)
print a
print d   
result > set([1, 2, 3, 4, 5])
         set([1, 3])
def difference_update(self, *args, **kwargs)
Remove all elements of another set from this set
清空a中所有b的元素,不返回任何数据,直接修改a
a.difference_update(b)
print a
set([1, 3])
def discard(self, *args, **kwargs)
Remove an element from a set if it is a member.
If the element is not a member, do nothing
从一个集合中清除某个元素,如果他不存在,也不报错
def intersection(self, *args, **kwargs)
返回一个新集合,求交集
def intersection_update(self, *args, **kwargs)
不返回集合,求交集,更新a
def isdisjoint(self, *args, **kwargs)
Return True if two sets have a null intersection
如果两个集合有空的交集时,返回True
def issubset(self, *args, **kwargs)
Report whether another set contains this set
报告另一个集合是否是他的子集
def issuperset(self, *args, **kwargs):
Report whether this set contains another set
报告另一个集合是否是他的父集
def pop(self, *args, **kwargs)
Remove and return an arbitrary set element.
        Raises KeyError if the set is empty.
随机删除集合中的一个元素,如果集合为空,报错
def remove(self, *args, **kwargs)
Remove an element from a set; it must be a member.
If the element is not a member, raise a KeyError.
移除一个集合中的元素,如果不是,报错
def symmetric_difference(self, *args, **kwargs)
求交集,并返回
def symmetric_difference_update(self, *args, **kwargs)
求交集,不返回,直接修改
def union(self, *args, **kwargs)
求并集,并返回
def update(self, *args, **kwargs)
求并集,不返回
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值