Python中怎么实现数学集合运算

发布时间:2021-07-05 17:56:36 作者:Leah
来源:亿速云 阅读:242

这篇文章将为大家详细讲解有关Python中怎么实现数学集合运算,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

‘union()’

返回一个新集合,其中包含该set和other集合中的元素,使用union()或“|“算子。

语法:

union(*others) set | other | ...

Python中怎么实现数学集合运算

示例1:找到两个集合的并集—A和B

返回一个包含集合A和集合B中的元素的新集合。但元素不会重复,集合中的所有元素都是唯一的。

A={1,2,3,4,5} B={2,4,6,8} print (A.union(B))#Output:{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} print (A|B|C)#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10} print (A.union(B,C))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

union()方法和|之间的区别:

示例3:在union()方法中将iterable用作参数

A={1,2,3,4,5} #iterable is given as list print (A.union([2,4,6]))#Output:{1, 2, 3, 4, 5, 6} #iterable is given as tuple print (A.union((2,4,6)))#Output:{1, 2, 3, 4, 5, 6} #iterable is given as range object print (A.union(range(5,10)))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9} #iterable is given as a dictionary print (A.union({'a':6,'b':7}))#Output:{1, 2, 3, 4, 5, 'b', 'a'}

示例4:为|提供参数iterable

A={1,2,3,4,5} B=[1,2,3] print (A|B) #Output:TypeError: unsupported operand type(s) for |: 'set' and 'list'

‘update()’

更新集合,并从other集合中添加元素,元素不会重复,集合中的所有元素都是唯一的。通过使用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: None print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}A={1,2,3,4,5} B={4,5,6,7,8} A|=B print (A) #Output: {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: None print (A) #Output: {1, 2, 3, 4, 5, 6, 7} A={1,2,3} B={3,4,5} C={5,6,7} A|=B|C print (A) #Output: {1, 2, 3, 4, 5, 6, 7}

update() 方法和|=运算符之间的区别:

示例3:在update() 方法中将iterable用作参数

A={1,2,3} #iterable is given as list print (A.update([2,3,4]))#Output:None print (A)#Output:{1,2,3,4} #iterable is given as tuple A={1,2,3} A.update((2,3,4)) print (A)#Output:{1,2,3,4} #iterable is given as range object A={1,2,3} A.update(range(2,5)) print (A)#Output:{1,2,3,4} #iterable is given as a dictionary A={1,2,3} A.update({2:'a',3:'b'}) print (A) #Output:{1, 2, 3}

示例4:为|=运算符提供参数iterable:

#iterable is given as tuple A={1,2,3} B=(3,4) A|=B#Output:TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'

‘intersection()’

返回一个具有该集合和other集合共同元素的新集合,通过intersection()或使用&运算符来执行。

语法:

intersection(*others) set & other & ...

Python中怎么实现数学集合运算

示例1:找到两个集合的交集—A和B

返回一个新集合,其中包含集合A和集合B中的共同元素。

A={1,2,3,4,5} B={2,4,6,8} #intersection is performed by intersection() method or & operator print (A.intersection(B))#Output:{2,4} print (A&B)#Output:{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()方法和&运算符之间的区别:

示例3:在intersection()方法中将iterable用作参数

A={1,2,3,4,5} #iterable is given as list print (A.intersection([1,4,6]))#Output:{1,4} #iterable is given as tuple print (A.intersection((2,4,6)))#Output:{2,4} #iterable is given as range object print (A.intersection(range(5,10)))#Output:{5} #iterable is given as a dictionary print (A.intersection({1:'a','b':7}))#Output:{1}

示例4:为&运算符提供参数iterable

A={1,2,3,4,5} B=[1,2,3] print (A&B) #Output:TypeError: unsupported operand type(s) for &: 'set' and 'list'

‘intersection_update()’

更新集合,只保留集合和other中共同的元素。可以通过使用  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: None print (A) #Output: {4,5}A={1,2,3,4,5} B={4,5,6,7,8} A&=B print (A) #Output: {4,5}

‘difference()’

返回一个去除other中元素之后的新集合,通过difference() 或使用-运算符来执行。

语法:

difference(*others) set - other - ...

5A9ce2dee869952e2e78.png" target="_blank"><img src="https://s1.51cto.com/oss/202009/30/7b2fc3539a675A9ce2dee869952e2e78.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()方法和-运算符之间的区别:

示例3:在difference()方法中将iterable作为参数

A={1,2,3,4,5} #iterable is given as list print (A.difference([1,2,3]))#Output:{4,5} #iterable is given as tuple print (A.difference((1,2,3)))#Output:{4,5} #iterable is given as range object print (A.difference(range(1,4)))#Output:{4,5} #iterable is given as a dictionary print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}

示例4:为-运算符提供参数iterable

A={1,2,3,4,5} B=[1,2,3] print (A-B) #Output:TypeError: unsupported operand type(s) for -: 'set' and 'list'

&lsquo;difference_update()&rsquo;

从other集合中删除该集合中的元素,通过使用-= 运算符或使用difference_update()  方法来执行,返回类型为None,将修改原始集本身。

句法:

difference_update(*others) set -= other | ...

Python中怎么实现数学集合运算

示例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()方法和-运算符之间的区别:

示例3:在difference()方法中将iterable作为参数

A={1,2,3,4,5} #iterable is given as list print (A.difference([1,2,3]))#Output:{4,5} #iterable is given as tuple print (A.difference((1,2,3)))#Output:{4,5} #iterable is given as range object print (A.difference(range(1,4)))#Output:{4,5} #iterable is given as a dictionary print (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}

示例4:为-运算符提供参数iterable

A={1,2,3,4,5} B=[1,2,3] print (A-B) #Output:TypeError: unsupported operand type(s) for -: 'set' and 'list'

&lsquo;difference_update()&rsquo;

从other集合中删除该集合中的元素,通过使用-= 运算符或使用difference_update()  方法来执行,返回类型为None,将修改原始集本身。

句法:

difference_update(*others) set -= other | ...

Python中怎么实现数学集合运算

示例1:找到两个集合A和B之间的difference_update()

通过删除集合A和集合B中都存在的元素来更新集合A。

A={1,2,3,4,5} B={2,4,6} #Return type is None. print (A.difference_update(B))#Output:None #It will update the original set print (A) #Output: {1,3,5} # difference_update by using -= operator A-=(B) print (A) #Output: {1,3,5}

示例2:查找两个以上集合之间的difference_update

#difference_update() will modify the original set. A={1,2,3} B={1} C={2} #Return type is None. print (A.difference_update(B,C))#Output:None #It will update the original set print (A) #Output: {3} # difference_update by using -= operator A={1,2,3} B={1} C={2} A-=B|C print (A) #Output: {3}

difference_update()方法与-=运算符的区别:

示例3:在difference_update()方法中将iterable作为参数

#iterable is given as list A={1,2,3} B=[1] print (A.difference_update(B))#Output:None print (A)#Output:{2,3}

示例4:为-=运算符提供参数iterable

A={1,2,3} B=[1] A-=Bprint (A)#Output: TypeError: unsupported operand type(s) for -=: 'set' and 'list'

&lsquo;symmetric_difference()&rsquo;

返回一个新集合,该集合中的元素属于集合或other,但不包含两个集合共有的元素。通过symmetric_difference()或使用^运算符来执行。

语法:

symmetric_difference(other) set ^ other

Python中怎么实现数学集合运算

示例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() takes exactly one argument (2 given)

但是我们可以使用^找到多个集合的对称差集

A={1,2} B={2,3,5} C={3,4} print (A^B^C)#Output:{1,4,5}

symmetric_difference()方法和^运算符之间的区别:

示例3:在symmetric_difference()方法中将iterable作为参数

#iterable is given as list A={1,2,3} B=[1] print (A.symmetric_difference(B))#Output:{2,3} #iterable is given as tuple A={1,2,3} B=(1,) print (A.symmetric_difference(B))#Output:{2,3} #iterable is given as range object A={1,2,3} B=range(2) print (A.symmetric_difference(B))#Output:{2,3}

示例4:为^运算符提供参数iterable:

A={1,2,3} B=[1] A^Bprint (A) #Output: TypeError: unsupported operand type(s) for ^: 'set' and'list'

&lsquo;symmetric_difference_update()&rsquo;

更新集合,保留在两个集合中均找到的元素并去除两个集合中的公共元素。可以通过使用symmetric_difference_update()或使用^=运算符来实现,返回类型为None,将修改原始集本身。

语法:

symmetric_difference_update(other) set ^= other

示例1:在A和B两组之间找到symmetric_difference_update()

将通过仅保留能在任一集合中找到,但不在两个集合中同时出现的元素来更新集合A。

#symmetric_difference_update() A={1,2,3,4,5} B={4,5,6,7,8} print (A.symmetric_difference_update(B)) #Output: None print (A) #Output: {1, 2, 3, 6, 7, 8} A={1,2,3,4,5} B={4,5,6,7,8} A^=B print (A) #Output: {1, 2, 3, 6, 7, 8}

&lsquo;isdisjoint()&rsquo;

如果该集合没有共同元素,则返回True。当且仅当它们的交集为空集时,这时称集合之间无连接。

语法:

isdisjoint(other)

5A14c6f7f809.png" target="_blank"><img src="https://s2.51cto.com/oss/202009/30/050f3a69e1f8f6f3bd675A14c6f7f809.png" alt="" title="" width="auto" height="auto" border="0">

示例

#Set A and Set B containing common elements A={1,2,3,4,5} B={4,5,6,7,8} print (A.isdisjoint(B))#Output:False #Set A and Set B not containing common elements A={1,2} B={3,4} print (A.isdisjoint(B))#Output:True

&lsquo;issubset()&rsquo;

测试集合中的每个元素是否都在other元素中。

语法:

issubset(other) set <= other

Python中怎么实现数学集合运算

示例:检查集合A是否为集合B的子集

可以通过issubset()方法或使用&le;运算符来完成。

A={1,2,3,4,5} B={4,5,6,7,8} print (A.issubset(B)) #Output: False print (A<=B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A.issubset(B)) #Output: True print (A<=B)#Output: False

Proper subset

测试集合是否为other的真子集,即set <= otherand set != other。

句法:

set < other

示例:检查集合A是否是B的真子集

如果两个集合相等,则意味着 A.issubsetset(B) 返回True,但是真子集A

A={1,2,3,4,5} B={4,5,6,7,8} print (A<B)#Output: False A={1,2,3,4,5} B={1,2,3,4,5} print (A<B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A<B)#Output: True

&lsquo;issuperset()&rsquo;

测试other中的每一个元素是否在集合中。

语法:

issuperset(other) set >= other

Python中怎么实现数学集合运算

示例:检查集合A是否为B的超集

可以通过issuperset()方法或使用&ge;运算符来实现:

A={1,2,3,4,5} B={4,5,6,7,8} print (A.issuperset(B)) #Output: False print (A>=B)#Output:True A={1,2,3,4,5} B={1,2,3} print (A.issuperset(B)) #Output: True print (A>=B)#Output:True

Proper superset

测试集合是否是other集合的真超集,即,set >= otherand set != other。

语法:

set > other

示例:检查集合A是否为B的真超集。

如果两个集合相等,则意味着A.issuperset(B)返回True,但是真超集A> B将返回False。

A={1,2,3,4,5} B={4,5} print (A>B)#Output: True A={1,2,3,4,5} B={1,2,3,4,5} print (A>B)#Output: False A={1,2,3} B={1,2,3,4,5} print (A>B)#Output: True

总结

Python中怎么实现数学集合运算

关于Python中怎么实现数学集合运算就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. python实现简单的数学小程序
  2. python3-基本数学

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:zookeeper的通知机制是什么

下一篇:python中prettyprint和pprint的作用是什么

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》