Python之set集合的相关介绍
发布时间:2025-10-24 16:06:05

了解python中的set集合及其用法
在python中,集合(set)这是一个无序的排列,但哈希支持集合关系测试,不支持索引和切片操作,没有特定的语法格式,只能通过工厂函数创建。集合中不会出现两个相同的元素,因此集合通常用于重新操作字符串或元组或列表中的元素。
以下语法可用于生成一个集合:
生成集合语法1:
>>>l1=[1,2,3,4,5,6]
>>>s1=set(l1)
>>>print(s1)
{1,2,3,4,5在这里,使用工厂函数set创建集合,set参数可以是列表,也可以是元组或字符串。
生成集合语法2:
>>>s2={6,7,8,9,10}
>>>print(s2)
{8,9,10,6,7生成集合语法3:
>>>s3={iforiinrange(10)}
>>>print(s3)
{0,2,3,4,5,6,7,8集合类型的方法和操作:
add
为集合增加一个元素,如果集合中已经存在,对集合没有影响 Addanelementtoaset. Thishasnoeffectiftheelementisalreadypresent.
>>>S1={1,2,3,4,5,6,7}
>>>s1.add(8)
>>>print(s1)
{1,2,3,4,5,6,7
>>>s1.add(9)
>>>print(s1)
{1,2,3,4,5,6,7,8clear
清空集合中所有元素的所有元素 Removeallelementsfromthisset.
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9}
>>>s1.clear()
>>>print(s1)
set()
>>>s2.clear()
>>>print(s2)
set()copy
浅复制集合(只复制元素,不复制内存地址) Returnashallowcopyofaset.
>>>S1={1,2,3,4,5,6,7}
>>>print(s1,id(s1))
140509840472
>>>s2=s1.copy()
>>>print(s2,id(s2))
{1,2,3,4,5,6,7}140509842716712272712difference
要求两个或多个集合的差集,并返回一个新集合 Returnthedifferenceoftwoormoresetsasanewset.
>>>s1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s1.difference(s2)
{1,2,3,4}
>>>s2.difference(s1)
{8,9,10}difference_update
从集合中移除两个集合的交集部分 Removeallelementsofanothersetfromthisset.
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s1.difference_update(s2)
>>>print(s1)
{1,2,3,4}
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s2.difference_update(s1)
>>>print(s2)
{8,9,10}相关推荐:Python视频教程
discard
从集合中移除一个元素,如果被移除的元素不在集合中,就不会报错 Removeanelementfromasetifitisamember. Iftheelementisnotamember,donothing.
{1,2,3,4,5,6
>>>s1.discard(7)
>>>print(s1)
{1,2,3,4,5
>>>s1.discard(4)
>>>print(s1)
{1,2,3,5,6}
>>>print(s1)
{1,2,3,5,6}intersection
在两个或两个以上的集合中寻求交集 Returntheintersectionoftwosetsasanewset.
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s1.intersection(s2)
{5,6,7}
>>>s2.intersection(s1)
{5,6,7}intersection_update
把两个集合的交集作为新的集合 Updateasetwiththeintersectionofitselfandanother.
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s1.intersection_update(s2)
>>>print(s1)
{5,6,7}
>>>print(s2)
{5,7,8,9,10}
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s2.intersection_update(s1)
>>>print(s2)
{5,6,7}
>>>print(s1)
{1,2,3,4,5,6isdisjoint
两集没有交集,回到True ReturnTrueiftwosetshaveanullintersection.
>>>S1={1,2,3,4,5,6,7}
>>>s2={5,6,7,8,9,10}
>>>s1.isdisjoint(s2)
False
>>>s1={1,2,3,4}
>>>s2={6,7,8,9}
>>>s1.isdisjoint(s2)
Trueissubset
若此集合是参数集合的子集,请返回True Reportwhetheranothersetcontainsthisset.
>>>s1={1,2,3,4}
>>>s2={1,2,3,4,5,6,7}
>>>s1.issubset(s2)
True
>>>s2.issubset(s1)
Falseissuperset
如果这一集是参数集的超集,返回True Reportwhetherthissetcontainsanotherset.
>>>s1={1,2,3,4}
>>>s2={1,2,3,4,5,6,7}
>>>s1.issuperset(s2)
False
>>>s2.issuperset(s1)
Truepop
从集合中删除一个元素,如果集合为空,则报错 Removeandreturnanarbitrarysetelement. RaisesKeyErrorifthesetisempty.
>>>s1={2,3,4,5}
>>>s1.pop()
2
>>>print(s1)
{3,4,5}
>>>s1.pop()
3
>>>s1.pop()
4
>>>s1.pop()
5
>>>s1.pop()
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<module>
KeyError:'popfromanemptyset'remove
移除集合中的一个元素,如果集合是空的,则报错 Removeanelementfromaset;itmustbeamember. Iftheelementisnotamember,raiseaKeyError.
>>>s1={1,2,3,4,5,6}
>>>s1.remove(4)
>>>print(s1)
{1,2,3,5,6}
>>>s1.remove(9)
Traceback(mostrecentcalllast):
File"<stdin>",line1,in<module>
KeyError:9symmetric_difference
返回两个集合的对称差集 Returnthesymmetricdifferenceoftwosetsasanewset.
>>>s1={1,2,3,4}
>>>s2={6,7,8,9}
>>>s1.symmetric_difference(s2)
{1,2,3,4,6,7,8
>>>s3={1,2,3,4,5
>>>s4={5,6,7,9,10}
>>>s3.symmetric_difference(s4)
{1,2,3,4,7,9,10}symmetric_difference_update
对称差集与参数集合,并返回给自己 Updateasetwiththesymmetricdifferenceofitselfandanother.
>>>s1={1,2,3,4}
>>>s2={6,7,8,9}
>>>s2.symmetric_difference_update(s1)
>>>print(s2)
{1,2,3,4,6,7,8
>>>s3={1,2,3,4,5
>>>s4={5,6,7,9,10}
>>>s3.symmetric_difference_update(s4)
>>>print(s3)
{1,2,3,4,7,9,10}union
两个或两个以上集合并集 Returntheunionofsetsasanewset.
>>>s1={1,2,3,4,5,6}
>>>s2={5,6,7,8,9}
>>>s1.union(s2)
{1,2,3,4,5,6,7,8
>>>s3={1,2,3,4}
>>>s4={6,7,8,9
>>>s3.union(s4)
{1,2,3,4,6,7,8update
与另一个集合并集,并返回给自己 Updateasetwiththeunionofitselfandothers.
>>>s3={1,2,3,4}
>>>s4={6,7,8,9
>>>s3.update(s4)
>>>print(s3)
{1,2,3,4,6,7,8 