python 集合运算
Python中的数学集合运算 (Mathematical Set Operations in Python)
Python’s set is an unordered collection in Python. It can be used to compute standard math operations, such as intersection, union, difference, and symmetric difference. Other collections — like list, tuple, and dictionary — don’t support set operations. Dict view objects are set-like, which allows set operations. Refer to my story on Python sets.
Python的set是Python中的无序集合。 它可以用于计算标准数学运算,例如交集,并集,差和对称差。 Other集合(例如列表,元组和字典)不支持集合操作。 Dict视图对象类似于集合,可以进行集合操作。 请参阅我关于Python集的故事。
This article will explore the mathematical operations supported by set objects in detail.
本文将详细探讨set对象支持的数学运算。
让我们看一下Set对象支持的数学运算 (Let's Look at the Mathematical Operations Supported by the Set Object)
union() union() update() update() intersection() intersection() intersection_update() intersection_update() difference() difference() difference_update() difference_update() symmetric_difference() symmetric_difference() symmetric_difference_update() symmetric_difference_update() isdisjoint() isdisjoint() issubset() issubset() issuperset() issuperset()
Set operations can be done in two ways. By using the method or by using an operator.
设置操作可以通过两种方式完成。 通过使用该方法或使用一个运算符。
'联盟()' (‘union()’)
Return a new set with elements from the set and the other. It’s performed by union() or by using the | operator
返回一个新的集合 与集合中的元素和other 。 它是由union()或使用| 算子
Syntax
句法
union(*others)
union ( *others )
set | other | ...
set | other | ...
‘union()’
'联盟()'
Example 1: Find the union of two sets — A and B
示例1:找到两个集合 A 并 集— A 和 B
It’ll return a new set containing elements from set A and set B. But it won’t repeat elements. All elements in the set are unique.
它将返回一个新集合,其中包含来自集合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}
Example 2: Find the union of more than two sets
示例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}
Difference between the union() method and the | operator:
union()方法和|之间的区别 操作员:
union(): It’ll accept any iterable as an argument union() :它将接受任何可迭代的参数 | operator: It’ll accept only a set as an argument. Otherwise, it’ll raise a TypeError. | 运算符:它仅接受一个set作为参数。 否则,它将引发TypeError 。
Example 3: Giving iterable as an argument in the union() method
示例3: 在 union() 方法 中将 iterable 用作参数
A={1,2,3,4,5}#iterable is given as listprint (A.union([2,4,6]))#Output:{1, 2, 3, 4, 5, 6}#iterable is given as tupleprint (A.union((2,4,6)))#Output:{1, 2, 3, 4, 5, 6}#iterable is given as range objectprint (A.union(range(5,10)))#Output:{1, 2, 3, 4, 5, 6, 7, 8, 9}#iterable is given as a dictionaryprint (A.union({'a':6,'b':7}))#Output:{1, 2, 3, 4, 5, 'b', 'a'}
Example 4: Giving iterable as an argument for the| operator
示例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()' (‘update()’)
It updates the set, adding elements from the other. But it won’t repeat elements. All elements in the set are unique. It’s performed by using update() or by using the |= operator. The return type is None. It’ll modify the original set itself.
它更新集合,并从other元素中添加元素。 但这不会重复元素。 集合中的所有元素都是唯一的。 通过使用update()或使用|=运算符来执行。 返回类型为None 。 它将修改原始集本身。
Syntax
句法
update(*others)
update ( *others )
set |= other | ...
set |= other | ...
Example 1: Calling update() between two sets — A and B
示例1: 在两个集合之间 调用 update() - A 和 B
It’ll update set A by adding elements found in both sets.
它将通过添加两个集合中找到的元素来更新集合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) #Output: {1, 2, 3, 4, 5, 6, 7, 8}
Example 2: Calling update() between more than two sets