set是Python中的无序集合,可以用来计算标准的数学运算,如交集、并集、差集和对称差集,其他集合(如列表、元组和dictionary)不支持集合操作,Dict视图对象类似于集合,可以进行集合操作。本文将详细讨论集合对??象支持的数学运算。我们来看看Set对象支持的数学运算:union()update()intersection()intersection_update()difference()difference_update()symmetric_difference()symmetric_difference_update()isdisjoint()issubset()issuperset()这样就搞定了有两种方式:使用方法或运算符。'union()'使用union()或“|”返回一个新集合,其中包含此集合和其他集合中的元素操作员。语法:union(*others)set|other|...例1:求两个集合的并集——A和B返回一个包含集合A和集合B中元素的新集合。但是元素不会重复,所有元素在集合中是独一无二的。A={1,2,3,4,5}B={2,4,6,8}print(A.union(B))#输出:{1,2,3,4,5,6,8}print(A|B)#Output:{1,2,3,4,5,6,8}示例2:求两个以上集合的并集A={1,2,3,4,5}B={2,4,6,8,10}C={1,3,5,7,9}打印(A|B|C)#输出:{1,2,3,4,5,6,7,8,9,10}print(A.union(B,C))#输出:{1,2,3,4,5,6,7,8,9,10}在union()方法和|之间差异:union():接受任何可迭代的参数|运算符:仅接受set作为参数,否则引发TypeError。示例3:在union()方法中使用可迭代对象作为参数A={1,2,3,4,5}#iterableisgivenaslistprint(A.union([2,4,6]))#Output:{1,2,3,4,5,6}#iterableisgivenastupleprint(A.union((2,4,6)))#Output:{1,2,3,4,5,6}#iterableisgivenasrangeobjectprint(A.union(范围(5,10)))#Output:{1,2,3,4,5,6,7,8,9}#iterableisgivenasadictionaryprint(A.union({'a':6,'b':7}))#Output:{1,2,3,4,5,'b','a'}示例4:为|提供参数iterableA={1,2,3,4,5}B=[1,2,3]print(A|B)#Output:TypeError:unsupportedoperandtype(s)for|:'set'and'list''update()'更新集合并从另一个集合中添加元素,元素不??会重复,集合中的所有元素都是唯一的。通过使用update()或使用|=运算符执行,返回类型为None,将修改原始集本身。语法:update(*others)set|=other|...例1:在两个集合A和B之间调用update(),通过在两个集合中添加元素来更新集合A。#update()A={1,2,3,4,5}B={4,5,6,7,8}print(A.update(B))#Output:Noneprint(A)#Output:{1,2,3,4,5,6,7,8}A={1,2,3,4,5}B={4,5,6,7,8}A|=Bprint(A)#输出:{1,2,3,4,5,6,7,8}示例2:在两个以上的集合之间调用update()#update()A={1,2,3}B={3,4,5}C={5,6,7}print(A.update(B,C))#Output:Noneprint(A)#Output:{1,2,3,4,5,6,7}A={1,2,3}B={3,4,5}C={5,6,7}A|=B|Cprint(A)#输出:{1,2,3,4,5,6,7}update()方法和|=运算符的区别:update():接受任何可迭代的参数。=运算符:只接受一个集合作为参数,否则将引发TypeError。示例3:在update()方法中使用iterable作为参数A={1,2,3}#iterableisgivenaslistprint(A.update([2,3,4]))#Output:Noneprint(A)#Output:{1,2,3,4}#iterableisgivenastupleA={1,2,3}A.update((2,3,4))print(A)#Output:{1,2,3,4}#iterableisgivenasrangeobjectA={1,2,3}A.update(range(2,5))print(A)#Output:{1,2,3,4}#iterableisgivenasadictionaryA={1,2,3}A.update({2:'a',3:'b'})print(A)#Output:{1,2,3}示例4:给|=运算符一个可迭代的参数:#iterableisgivenastupleA={1,2,3}B=(3,4)A|=B#Output:TypeError:unsupportedoperandtype(s)for|=:'set'and'tuple''intersection()'通过intersection()或使用&运算符来执行。语法:intersection(*others)set&other&...示例1:求两个集合A和B的交集返回包含集合A和集合B中公共元素的新集合。A={1,2,3,4,5}B={2,4,6,8}#intersectionisperformedbyintersection()methodor&operatorprint(A.intersection(B))#Output:{2,4}print(A&B)#输出:{2,4}示例2:求两个以上的交集A={1,2,3,4,5}B={2,4,6,8,10}C={2,4}print(A&B&C)#Output:{2,4}print(A.intersection(B,C))#Output:{2,4}intersection()方法和&运算符的区别:intersection():接受任意的迭代的参数。&运算符:只接受设置参数,否则会引发TypeError。示例3:在intersection()方法中使用可迭代对象作为参数A={1,2,3,4,5}#iterableisgivenaslistprint(A.intersection([1,4,6]))#Output:{1,4}#iterableisgivenastupleprint(A.intersection((2,4,6)))#Output:{2,4}#iterableisgivenasrangeobjectprint(A.intersection(range(5,10)))#Output:{5}#iterableisgivenasaddictionaryprint(A.intersection({1:'a','b':7}))#Output:{1}例4:提供参数iterableA={1,2,3,4,5}B=[1for&operator,2,3]print(A&B)#Output:TypeError:unsupportedoperandtype(s)for&:'set'and'list''intersection_update()'更新集合,只保留集合中公共元素等。可以通过使用intersection_update()或使用&=运算符执行,返回类型为None,这将修改原始集本身。语法:intersection_update(*others)set&=other&...示例1:查找两个集合A和B之间的交集intersection_update()通过仅保留在两个集合中找到的元素来更新集合A。#intersection_update()A={1,2,3,4,5}B={4,5,6,7,8}print(A.intersection_update(B))#Output:Noneprint(A)#Output:{4,5}A={1,2,3,4,5}B={4,5,6,7,8}A&=Bprint(A)#Output:{4,5}'difference()'返回通过difference()或使用-运算符删除了other中的元素的新集合。语法:difference(*others)set-other-...5A9ce2dee869952e2e78.png"target="_blank">5A9ce2dee869952e2e78.png"alt=""title=""width="auto"height="auto"border="0">示例1:找出两组A和B之间的差异返回一个新集合,其中包含集合A中但不在集合B中的元素。A={1,2,3,4,5}B={2,4,6,8}print(A.difference(B))#Output:{1,3,5}print(A-B)#Output:{1,3,5}例2:找出两个以上集合的差异A={1,2,3,4,5}B={2,4,6,8,10}C={2,3}print(A-B-C)#Output:{1,5}print(A.difference(B,C))#Output:{1,5}difference()方法与-运算符的区别:difference():接受任何可迭代参数-运算符:仅接受set作为参数。否则将引发TypeError。示例3:在difference()方法中使用可迭代对象作为参数A={1,2,3,4,5}#iterableisgivenaslistprint(A.difference([1,2,3]))#Output:{4,5}#iterableisgivenastupleprint(A.difference((1,2,3)))#Output:{4,5}#iterableisgivenasrangeobjectprint(A.difference(range(1,4)))#Output:{4,5}#iterableisgivenasaddictionaryprint(A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}例4:提供参数iterableA={1,2,3for-operator,4,5}B=[1,2,3]print(A-B)#Output:TypeError:unsupportedoperandtype(s)for-:'set'and'list''difference_update()'从其他集合中删除这个集合的元素,使用-=运算符或使用difference_update()方法执行,返回类型为None,将修改原始集合本身。语法:difference_update(*others)set-=other|...例1:找出两组A和B之间的差异,返回一个包含集合A中元素但不包含集合B中元素的新集合。A={1,2,3,4,5}B={2,4,6,8}print(A.difference(B))#输出:{1,3,5}print(A-B)#输出:{1,3,5}例2:找出两个以上集合之间的差异A={1,2,3,4,5}B={2,4,6,8,10}C={2,3}print(A-B-C)#Output:{1,5}print(A.difference(B,C))#Output:{1,5}difference()方法和-运算符的区别:difference():接受任何可迭代的参数-运算符:只接受集合作为参数。否则将引发TypeError。示例3:在difference()方法中使用可迭代对象作为参数A={1,2,3,4,5}#iterableisgivenaslistprint(A.difference([1,2,3]))#Output:{4,5}#iterableisgivenastupleprint(A.difference((1,2,3)))#Output:{4,5}#iterableisgivenasrangeobjectprint(A.difference(range(1,4)))#Output:{4,5}#iterableisgivenasaddictionaryprint(A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}例4:提供参数iterableA={1,2,3for-operator,4,5}B=[1,2,3]print(A-B)#Output:TypeError:unsupportedoperandtype(s)for-:'set'and'list''difference_update()'从其他集合中删除这个集合的元素,使用-=运算符或使用difference_update()方法执行,返回类型为None,将修改原始集合本身。语法:difference_update(*others)set-=other|...示例1:找出两个集合A和B之间的差异。difference_update()通过删除集合A和集合B中都存在的元素来更新集合A。A={1,2,3,4,5}B={2,4,6}#ReturntypeisNone.print(A.difference_update(B))#Output:None#Itwillupdatetheoriginalsetprint(A)#Output:{1,3,5}#difference_updatebyusing-=operatorA-=(B)print(A)#Output:{1,3,5}示例2:查找两个以上集合之间的difference_update#difference_update()willmodifytheoriginalset.A={1,2,3}B={1}C={2}#ReturntypeisNone.print(A.difference_update(B,C))#Output:None#Itwillupdatetheoriginalsetprint(A)#Output:{3}#difference_updatebyusing-=operatorA={1,2,3}B={1}C={2}A-=B|Cprint(A)#Output:{3}difference_update()方法和-=运算符的区别:difference_update():AcceptanyArguments对于迭代-=运算符:仅接受设置参数,否则将引发TypeError。示例3:在difference_update()方法中使用可迭代对象作为参数#iterableisgivenaslistA={1,2,3}B=[1]print(A.difference_update(B))#Output:Noneprint(A)#Output:{2,3}示例4:为-=运算符提供参数iterableA={1,2,3}B=[1]A-=Bprint(A)#Output:TypeError:unsupportedoperandtype(s)for-=:'set'and'list''symmetric_difference()'返回一个新集合,该集合包含属于set或other的元素,但不包含两个集合共有的元素。通过symmetric_difference()或使用^运算符强制执行。语法:symmetric_difference(other)set^other示例1:找出两个集合A和B之间的对称差异返回一个新集合,其中包含集合A和B中的元素,但不包含两个集合共有的元素。A={1,2}B={2,3}print(A.symmetric_difference(B))#Output:{1,3}print(A^B)#Output:{1,3}例2:对称差分集合仅适用于2个集合多个集合不支持symmetric_difference()方法,如果给出了两个以上的集合,则会引发TypeError。A={1,2}B={2,3,5}C={3,4}print(A.symmetric_difference(B,C))#Output:TypeError:symmetric_difference()takesexactlyoneargument(2given)但我们可以使用^求多个集合的对称差A={1,2}B={2,3,5}C={3,4}print(A^B^C)#Output:{1,4,5}的symmetric_difference()方法和^运算符之间的区别:symmetric_difference():接受任何可迭代的参数,但此方法不允许多个集合。^运算符:它将仅接受设置作为参数。否则,将引发TypeError。通过使用^运算符,可以找到多个集合之间的对称差异。示例3:可迭代作为symmetric_difference()方法中的参数#iterableisgivenaslistA={1,2,3}B=[1]print(A.symmetric_difference(B))#Output:{2,3}#iterableisgivenastupleA={1,2,3}B=(1,)print(A.symmetric_difference(B))#Output:{2,3}#iterableisgivenasrangeobjectA={1,2,3}B=range(2)print(A.symmetric_difference(B))#Output:{2,3}Example4:Provideargumentiterablefor^operator:A={1,2,3}B=[1]A^Bprint(A)#Output:TypeError:unsupportedoperandtype(s)for^:'set'and'list''symmetric_difference_update()'更新集合,保留在两个集合中找到的元素并删除两个集合中的公共元素。这可以通过使用symmetric_difference_update()或使用^=运算符来实现,返回类型为None,这将修改原始集本身。语法:symmetric_difference_update(other)set^=other示例1:在两组A和B之间查找#symmetric_difference_update()A={1,2,3,4,5}B={4,5,6,7,8}print(A.symmetric_difference_update(B))#Output:Noneprint(A)#Output:{1,2,3,6,7,8}A={1,2,3,4,5}B={4,5,6,7,8}A^=Bprint(A)#输出:{1,2,3,6,7,8}'isdisjoint()'如果集合没有公共元素,则返回True。当且仅当它们的交集是空集时,才称这些集是不连通的。语法:isdisjoint(other)5A14c6f7f809.png"target="_blank">5A14c6f7f809.png"alt=""title=""width="auto"height="auto"border="0">Example#SetAandSetBcontainingcommonelementsA={1,2,3,4,5}B={4,5,6,7,8}print(A.isdisjoint(B))#Output:False#SetAandSetBnotcontainingcommonelementsA={1,2}B={3,4}print(A.isdisjoint(B))#Output:True'issubset()'测试集合中的每个元素是否在另一个元素中。语法:issubset(other)set<=other示例:检查集合A是否是集合B的子集可以通过issubset()方法或使用≤运算符来完成。A={1,2,3,4,5}B={4,5,6,7,8}print(A.issubset(B))#Output:Falseprint(A<=B)#Output:FalseA={1,2,3}B={1,2,3,4,5}print(A.issubset(B))#Output:Trueprint(A<=B)#Output:FalseProper子集检验集合是否为other适当的子集,即set<=other和set!=other。语法:set
