union()、update()求并集
intersection()与intersection_update()的差别
intersection_update(…)
Update a set with the intersection of itself and another.
更新一?经由订交后的集合给本身。
issuperset()一个集合包含另一个集合
set和frozenset的混淆运算
留意:当我们欲望将两个对象订交后的结不雅更新给个一一个操尴尬刁难象时,建议应用intersection_update()函数,这个函数应用原位操作的办法实现,拥有更低的资本消费。然则intersection_update()函数是没有返回值的,即不克不及将结不雅赋值给一个新的变量。
- In [94]: s1
- Out[94]: {1, 2, 3, 'a', 'b'}
- In [95]: s2
- Out[95]: {3, 4, 5, 'b', 'c', 'd'}
- In [96]: s1.intersection_update(s2)
- In [97]: s1
- Out[97]: {3, 'b'}
与intersection()一样,可以传递不合的迭代器类型参数。
union() 返回并集
union(…)
Return the union of sets as a new set.
- In [108]: s4
- Out[108]: {1, 2, 3, 4, 5, 8, 9, 10, 'a', 'b', 'c', 'd', 'de', 'f'}
留意:可以应用逻辑或运算符 ‘|’
- In [109]: s4 = s1 | s2 | s3
- In [110]: s4
- Out[110]: {1, 2, 3, 4, 5, 8, 9, 10, 'a', 'b', 'c', 'd',
推荐阅读
作为办事的灾害恢复,是经由过程云来降低营业办事成本的好办法。在内部,灾害恢赋臣芏着在另一个地位创建临盆>>>详细阅读
本文标题:Python集合set和frozenset的内建方法详解
地址:http://www.17bianji.com/lsqh/35321.html
1/2 1