集合4:方法-仅适用于set

本文详细介绍了Python中仅适用于set集合的9种修改方法:update、intersection_update、difference_update、symmetric_difference_update、add、remove、discard、pop及clear。这些方法包括并集、交集、差集、对称差集的更新,元素的添加、移除以及集合的清空等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1. s.update(*others)

2. s.intersection_update(*others)

3. s.difference_update(*others)

4. s.symmetic_difference_update(other)

5. s.add(elem)

6. s.remove(elem)

7. s.discard(elem)

8. s.pop()

9. s.clear()


会对原集合做修改的方法,仅能用于set对象,而不能用于frozenset对象

1. s.update(*others)

使用 others 容器中的元素来更新集合,即更新s集合,内容为s和others的并集【参见5. 并集 】;

other可以是列表、元组、字典、字符串等,字典是由键的集合参与并集,字符串是单个字符做为元素参与并集;

*others 可以是多个参数。

#使用 others 容器中的元素来更新集合,即s被更新为s和others的并集
s1 = {1,2,3,4}
s2 = {0,0.1,-2j,(3,4),frozenset({5,6}),'789'}
s1.update(s2)
s1
{0, 1, 2, 3, 4, 0.1, (3, 4), frozenset({5, 6}), (-0-2j), '789'}

#*others 可以是多个参数
s1 = {1,2,3,4}
s3 = {'1',2.0,0.10,3j,4}
s1.update(s2,s3)
s1
{0, 1, 2, 3, 4, 0.1, (3, 4), '1', 3j, frozenset({5, 6}), (-0-2j), '789'}

#other可以是列表、元组、字典、字符串等
s1 = {1,2,3}
s2 = [0,2,4]
s3 = (1,3,5)
s4 = {2:'a',6:'b',10:'c'}
s5 = '1789'
s1.update(s2,s3,s4,s5)
s1
{0, 1, 2, 3, 4, 5, 6, '1', '7', '8', 10, '9'}

2. s.intersection_update(*others)

更新 s 集合,其内容是 s 集合与 others 容器的交集,【参见6. 交集】;

other可以是列表、元组、字典、字符串等,字典是由键的集合参与并集,字符串是单个字符做为元素参与并集;

*others 可以是多个参数。

s1 = {1,2,3,4,5}
s2 = {0,1.0,2j,3,(4,5),'6'}
s3 = {5,1,'abc',frozenset({1,2,3})}

#更新 s 集合,其内容是 s 集合与 others 容器的交集
s1.intersection_update(s2)
s1
{1, 3}
s1 = {1,2,3,4,5}
s1.intersection_update(s3)
s1
{1, 5}

#*others 可以是多个参数
s1 = {1,2,3,4,5}
s1.intersection_update(s2,s3)
s1
{1}

#other可以是列表、元组、字典、字符串等
s1 = {1,2,'3',4,5}
s2 = [0,1,2,'3','4']
s3 = ('3',(4,5),1,2)
s4 = {1:4,'3':5,2:'xxx',(5,):'yyy'}
s5 = '12345'
s1.intersection_update(s2,s3,s4)
s1
{1, 2, '3'}
s1 = {1,2,'3',4,5}
s1.intersection_update(s2,s3,s4,s5)
s1
{'3'}

3. s.difference_update(*others)

更新 s 集合,其内容是 s 集合与 others 容器的差集,【参见7. 差集】;

other可以是列表、元组、字典、字符串等,字典是由键的集合参与并集,字符串是单个字符做为元素参与并集;

*others 可以是多个参数。

s1 = {1,2,3,4,5}
s2 = {1,2.0,'3',(4,5),6,7,8}
s3 = {5,0,1,'s234'}

#更新 s 集合,其内容是 s 集合与 others 容器的差集
s1.difference_update(s2)
s1
{3, 4, 5}
s1 = {1,2,3,4,5}
s1.difference_update(s3)
s1
{2, 3, 4}

#*others 可以是多个参数
s1 = {1,2,3,4,5}
s1.difference_update(s2,s3)
s1
{3, 4}

#other可以是列表、元组、字典、字符串等
s1 = {1,2,3,4,5}
s1 = {1,2,'3',4,5}
s2 = [0,1.1,2.0,(4,5),'aaa']
s3 = (2,0,'45',1)
s4 = {'1':1,'4':4,'5':5,6:'6'}
s5 = '12345'
s1.difference_update(s2,s3,s4)
s1
{4, 5, '3'}
s1 = {1,2,'3',4,5}
s1.difference_update(s2,s3,s4,s5)
s1
{4, 5}

4. s.symmetic_difference_update(other)

更新 s 集合,其内容是排除掉 s 集合和 other 容器中共有的元素后,剩余的所有元素,【参见8. 对称差集】;

other可以是列表、元组、字典、字符串等,字典是由键的集合参与并集,字符串是单个字符做为元素参与并集;

该方法只能调用一个参数。

s1 = {1,2,3,4,5}
s2 = {5,'4',(1,2),3j,0,'abc'}
s3 = {1,3,5,7,9}

#更新 s 集合,其内容是排除掉 s 集合和 other 容器中共有的元素后,剩余的所有元素
s1.symmetric_difference_update(s2)
s1
{0, 1, 2, 3, 4, (1, 2), '4', 3j, 'abc'}
s1 = {1,2,3,4,5}
s1.symmetric_difference_update(s3)
s1
{2, 4, 7, 9}

#该方法只能调用一个参数。
s1 = {1,2,3,4,5}
s1.symmetric_difference_update(s2,s3)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.symmetric_difference_update() takes exactly one argument (2 given)

#other可以是列表、元组、字典、字符串等
s1 = {1,2,3,4,5}
s2 = [0,2,4,'5',6]
s1.symmetric_difference_update(s2)
s1
{0, 1, 3, 5, 6, '5'}

s1 = {1,2,3,4,5}
s3 = (frozenset({1,2,3}),4,5,6)
s1.symmetric_difference_update(s3)
s1
{1, 2, 3, 6, frozenset({1, 2, 3})}

s1 = {1,2,3,4,5}
s4 = {'1':'x',2:'y',3:'z','4':5}
s1.symmetric_difference_update(s4)
s1
{1, 4, 5, '1', '4'}

s1 = {'1',2,'3',4,'5'}
s5 = '12345'
s1.symmetric_difference_update(s5)
s1
{2, '4', 4, '2'}

5. s.add(elem)

将 elem 元素添加到 s 集合中;

elem需要满足集合元素要求,即参考可哈希:hashable ;

.add()只能添加一个参数,少于或大于1个都报错,添加参数无序插入s集合中。

s1 = {1,2,3}

#添加整型元素
s1.add(4)
s1
{1, 2, 3, 4}

#添加字符串元素
s1.add('5')
s1
{1, 2, 3, 4, '5'}

#添加元组元素
s1.add(('67','8'))
s1
{1, 2, 3, 4, '5', ('67', '8')}

#添加冻结集合元素
s1.add(frozenset({9,10}))
s1
{1, 2, 3, 4, frozenset({9, 10}), '5', ('67', '8')}

#添加浮点数元素
s1.add(10.1)
s1
{1, 2, 3, 4, frozenset({9, 10}), 10.1, '5', ('67', '8')}

#添加复数元素
s1.add(-11j)
s1
{1, 2, 3, 4, frozenset({9, 10}), 10.1, '5', ('67', '8'), (-0-11j)}

#只能添加1个元素,否则报错
s1.add(10.1,11j)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.add() takes exactly one argument (2 given)
s.add()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.add() takes exactly one argument (0 given)

6. s.remove(elem)

将 elem 元素从 s 集合中移除;

如果不存在该元素,则抛出 KeyError 异常;

只能输入1个elem元素,元素数量小于1或大于1,报错。

s = {0,1.1,-2j,(3,'xxx'),frozenset({'yyy',4}),'567abc'}

#只能输入1个elem元素,元素数量小于1或大于1,报错
s.remove()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.remove() takes exactly one argument (0 given)
s.remove(0,1.1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.remove() takes exactly one argument (2 given)

#移除整数
s.remove(0)
s

#移除冻结集合
{1.1, frozenset({'yyy', 4}), (3, 'xxx'), '567abc', (-0-2j)}
s.remove({'yyy',4})
s
{1.1, (3, 'xxx'), '567abc', (-0-2j)}

#如果不存在该元素,则抛出 KeyError 异常
s.remove((3,))
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: (3,)

#移除元组
s.remove((3,'xxx'))
s
{1.1, '567abc', (-0-2j)}

#移除浮点数
s.remove(1.1)
s
{'567abc', (-0-2j)}

#移除复数
s.remove(-2j)
s
{'567abc'}

#如果不存在该元素,则抛出 KeyError 异常
s.remove('567')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: '567'

#移除字符串元素,集合为空
s.remove('567abc')
s
set()

7. s.discard(elem)

将 elem 元素添加到 s 集合中移除;

即如果s集合中不存在elem元素,先添加elem元素再移除,原集合中没有elem元素也不报错;

只能输入1个elem元素,元素数量小于1或大于1,报错。

s = {0,1.1,-2j,(3,'xxx'),frozenset({'yyy',4}),'567abc'}

#只能输入1个elem元素,元素数量小于1或大于1,报错
s.discard()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.discard() takes exactly one argument (0 given)
s.discard(1.1,0)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: set.discard() takes exactly one argument (2 given)

#移除整数
s.discard(0)
s
{1.1, frozenset({'yyy', 4}), (3, 'xxx'), '567abc', (-0-2j)}

#移除集合中不存在的冻结集合,不报错,冻结集合未删除
s.discard({'yyy'})
s
{1.1, frozenset({'yyy', 4}), (3, 'xxx'), '567abc', (-0-2j)}

#移除冻结集合
s.discard({'yyy',4})
s
{1.1, (3, 'xxx'), '567abc', (-0-2j)}

#移除集合中不存在的字符串,不报错,字符串未删除
s.discard('567')
s
{1.1, (3, 'xxx'), '567abc', (-0-2j)}

#移除字符串
s.discard('567abc')
s
{1.1, (3, 'xxx'), (-0-2j)}

#移除复数
s.discard(-2j)
s
{1.1, (3, 'xxx')}

#移除集合中不存在的元组,不报错,字符串未删除
s.discard(('xxx',))
s
{1.1, (3, 'xxx')}

#移除元组
s.discard((3,'xxx'))
s
{1.1}

#移除浮点数,集合数据全部移除为空
s.discard(1.1)
s
set()

8. s.pop()

从 s 集合中移除并返回任意一个元素,移除顺序随机;

如果集合为空,则抛出 KeyError 异常。

s = {0,1.1,-2j,(3,'xxx'),frozenset({'yyy',4}),'567abc'}

#从 s 集合中移除并返回任意一个元素,移除顺序随机
s.pop()
0
s.pop()
1.1
s.pop()
frozenset({'yyy', 4})
s.pop()
(3, 'xxx')
s.pop()
'567abc'
s.pop()
(-0-2j)

#如果集合为空,则抛出 KeyError 异常
s.pop()
Traceback (most recent call last):
  File "<input>", line 1, in <module>
KeyError: 'pop from an empty set'

9. s.clear()

删除 s 集合中的所有元素。

s = {0,1.1,-2j,(3,'xxx'),frozenset({'yyy',4}),'567abc'}
s.clear()
s
set()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

燃烧的火鸟啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值