集合方法

set.isdisjoint 交集为空吗?

集合方法 set.isdisjoint(),Python 官方文档描述如下:

help(set.isdisjoint)
Help on method_descriptor:

isdisjoint(...)
    Return True if two sets have a null intersection.

两个集合的交集为空,则返回 True。方法中的参数可以是可迭代对象。

{1,2}.isdisjoint([1,2])
False
{1,2}.isdisjoint([3,4])
True
{1,2}.isdisjoint('12') # 参数是字符串而非数字
True

set.issubset 是子集吗?

集合方法 set.issubset(),Python 官方文档描述如下:

help(set.issubset)
Help on method_descriptor:

issubset(...)
    Report whether another set contains this set.

检查一个集合中的元素,是否都在另一个集合中。相当于 a <= b(a,b 是两个集合),但方法中的参数可以是可迭代对象。

{1,2}.issubset([1,2,3])
True
{1,2} <= {1,2,3}
True
{1,2}.issubset((1,1,2))
True

该方法检查是否是子集,而对于真子集,可以使用 < 进行检查:

{1,2} < {1,1,2}
False
{1,2} < {1,2,3}
True

set.issuperset 是超集吗?

集合方法 set.issuperset(),Python 官方文档描述如下:

help(set.issuperset)
Help on method_descriptor:

issuperset(...)
    Report whether this set contains another set.

检查一个集合是否是另一个集合的超集。相当于 a >= b(a,b 是两个集合),但方法中的参数可以是可迭代对象。

{1,2}.issuperset({True:'1',2:'2'})
True
{1,2,3} >= {True,2}
True

对于真超集,使用 > 进行检查:

{1,2,3} > {True,2}
True
{1,2} > {True,2}
False

set.union 并集

集合方法 set.union(),Python 官方文档描述如下:

help(set.union)
Help on method_descriptor:

union(...)
    Return the union of sets as a new set.
    
    (i.e. all elements that are in either set.)

该方法接收任意的位置参数,返回一个所有集合的元素组成的新集合。相当于 a | b | … (a,b 是集合),但方法中的参数可以是可迭代对象。

{1}.union([2],(3,4))
{1, 2, 3, 4}
{1} | {2} | {3,4}
{1, 2, 3, 4}

set.intersection 交集

集合方法 set.intersection(),Python 官方文档描述如下:

help(set.intersection)
Help on method_descriptor:

intersection(...)
    Return the intersection of two sets as a new set.
    
    (i.e. all elements that are in both sets.)

该方法接收任意的位置参数,返回一个所有集合中共有的元素组成的新集合。相当于 a & b & … (a,b 是集合),但方法中的参数可以是可迭代对象。

{1,2,3}.intersection({3,4},[1,5,3])
{3}
{1,2,3} & {3,4} & {1,5,3}
{3}

set.difference 差集

集合方法 set.difference(),Python 官方文档描述如下:

help(set.difference)
Help on method_descriptor:

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

该方法接收任意的位置参数,返回一个集合在其他所有集合中都不存在的元素组成的新集合。相当于 a - b - … (a,b 是集合),但方法的参数可以是可迭代对象。

{1,2,3,4}.difference((2,1,5),{6,2,4})
{3}
{1,2,3,4} - {2,1,5} - {6,2,4}
{3}

set.symmetric_difference 对称差

集合方法 set.symmetric_difference(),Python 官方文档描述如下:

help(set.symmetric_difference)
Help on method_descriptor:

symmetric_difference(...)
    Return the symmetric difference of two sets as a new set.
    
    (i.e. all elements that are in exactly one of the sets.)

返回两个集合中非共同元素组成的新集合。相当于 a ^ b(a,b 是集合),但方法的参数可以是可迭代对象。

{1,2,3}.symmetric_difference([2,3,4])
{1, 4}
{1,2,3} ^ {2,3,4}
{1, 4}

set.copy 浅拷贝

集合方法 set.copy(),Python 官方文档描述如下:

help(set.copy)
Help on method_descriptor:

copy(...)
    Return a shallow copy of a set.

返回集合的一个浅拷贝。

a = {1,2}
print(id(a),a)
b = a.copy()
id(b),b
2091426558696 {1, 2}





(2091426559368, {1, 2})

set.update 合并更新

集合方法 set.update(),Python 官方文档描述如下:

help(set.update)
Help on method_descriptor:

update(...)
    Update a set with the union of itself and others.

该方法接收任意的位置参数,将其他集合的元素合并到一个集合中。相当于 a |= b | … (a,b 是集合),但方法的参数可以是可迭代对象。

a = {1,2}
a.update([3],(4,5))
a
{1, 2, 3, 4, 5}
a = {1,2}
a |= {3} | {4,5}
a
{1, 2, 3, 4, 5}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2}
b = a.update([3],(4,5))
print(b)
None

set.intersection_update 交集更新

集合方法 set.intersection_update(),Python 官方文档描述如下:

help(set.intersection_update)
Help on method_descriptor:

intersection_update(...)
    Update a set with the intersection of itself and another.

该方法接收任意的位置参数,更新集合,只保留在其他所有集合中都存在的元素。相当于 a &= b & …(a,b 是集合),但方法的参数可以是可迭代对象。

a = {1,2,3,4}
a.intersection_update((1,2,5),{1,3,5})
a
{1}
a = {1,2,3,4}
a &= {1,2,5} & {1,3,5}
a
{1}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2,3,4}
b = a.intersection_update()
print(b)
None

set.difference_update 差集更新

集合方法 set.difference_update(),Python 官方文档描述如下:

help(set.difference_update)
Help on method_descriptor:

difference_update(...)
    Remove all elements of another set from this set.

该方法接收任意的位置参数,更新集合,移除在其他集合中也存在的元素。相当于 a -= b | …(a,b 是集合),但方法的参数可以是可迭代对象。

a = {1,2,3,4}
a.difference_update((1,3,5),[4,5])
a
{2}
a = {1,2,3,4}
a -= {1,3,5} | {4,5}
a
{2}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2,3,4}
b = a.difference_update([4,5])
print(b)
None

set ^= other 对称差集更新

集合的对称差集更新操作,相当于集合方法 set.symmetric_difference_update,其文档描述如下:

help(set.symmetric_difference_update)
Help on method_descriptor:

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

更新集合,只保留两个集合中非共同部分。

a = {1,2,3}
a.symmetric_difference_update([2,3,4])
a
{1, 4}
a = {1,2,3}
a ^= {2,3,4}
a
{1, 4}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2,3}
b = a.symmetric_difference_update([2,3,4])
print(b)
None

set.add 添加元素

集合方法 set.add(),Python 官方文档描述如下:

help(set.add)
Help on method_descriptor:

add(...)
    Add an element to a set.
    
    This has no effect if the element is already present.

集合中增加一个元素,如果元素已经存在,没有任何影响。

a = {1,2}
a.add(3)
a
{1, 2, 3}
a = {1,2}
a.add(1)
a
{1, 2}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2}
b = a.add(3)
print(b)
None

set.remove 删除元素

集合方法 set.remove(),Python 官方文档描述如下:

help(set.remove)
Help on method_descriptor:

remove(...)
    Remove an element from a set; it must be a member.
    
    If the element is not a member, raise a KeyError.

删除一个指定元素,删除元素不存在则引发 KeyError。

a = {1,2}
a.remove(1)
a
{2}
a = {1,2}
a.remove(3)
a
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-3-d0f57d460301> in <module>
      1 a = {1,2}
----> 2 a.remove(3)
      3 a


KeyError: 3

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2}
b = a.remove(1)
print(b)
None

set.discard 删除元素

集合方法 set.discard(),Python 官方文档描述如下:

help(set.discard)
Help on method_descriptor:

discard(...)
    Remove an element from a set if it is a member.
    
    If the element is not a member, do nothing.

从集合中删除一个指定元素,元素不存在没有任何影响。

a = {1,2}
a.discard(1)
a
{2}
a = {1,2}
a.discard(3)
a
{1, 2}

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2}
b = a.discard(1)
print(b)
None

set.pop 删除元素并返回

集合方法 set.pop(),Python 官方文档描述如下:

help(set.pop)
Help on method_descriptor:

pop(...)
    Remove and return an arbitrary set element.
    Raises KeyError if the set is empty.

集合中删除任意一个元素,并返回它。如果集合为空,引发 KeyError。

a = {1,2}
a.pop()
1
a
{2}
set().pop()
---------------------------------------------------------------------------

KeyError                                  Traceback (most recent call last)

<ipython-input-6-db3bab0ab3b8> in <module>
----> 1 set().pop()


KeyError: 'pop from an empty set'

set.clear 清空集合元素

集合方法 set.clear(),Python 官方文档描述如下:

help(set.clear)
Help on method_descriptor:

clear(...)
    Remove all elements from this set.

清空集合所有元素。

a = {1,2}
a.clear()
a
set()

该方法是一个过程,就地修改集合,返回值为 None。

a = {1,2}
b = a.clear()
print(b)
None
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值