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 | ...
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 argumentunion()
:它将接受任何可迭代的参数|
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.|
运算符:它仅接受一个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 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'}
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|=B
print (A) #Output: {1, 2, 3, 4, 5, 6, 7, 8}
Example 2: Calling update()
between more than two sets
示例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|C
print (A) #Output: {1, 2, 3, 4, 5, 6, 7}
Difference between the update()
method and the |=
operator:
update()
方法和|=
运算符之间的区别:
update()
: It’ll accept any iterable as an argumentupdate()
:它将接受任何可迭代的参数|=
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.|=
运算符:它将仅接受set作为参数。 否则,它将引发TypeError
。
Example 3: Giving iterable
as an argument in the update()
method
示例3: 在 update()
方法 中将 iterable
用作参数
A={1,2,3}#iterable is given as listprint (A.update([2,3,4]))#Output:Noneprint (A)#Output:{1,2,3,4}
#iterable is given as tupleA={1,2,3}
A.update((2,3,4))
print (A)#Output:{1,2,3,4}
#iterable is given as range objectA={1,2,3}
A.update(range(2,5))
print (A)#Output:{1,2,3,4}
#iterable is given as a dictionaryA={1,2,3}
A.update({2:'a',3:'b'})
print (A) #Output:{1, 2, 3}
Example 4: Giving iterable
as an argument for the |=
operator
示例4: 为 |=
运算符 提供 iterable
作为参数
#iterable is given as tupleA={1,2,3}
B=(3,4)
A|=B#Output:TypeError: unsupported operand type(s) for |=: 'set' and 'tuple'
'路口()' (‘intersection()’)
Return a new set with elements common to the set and the other
. It’s performed by intersection()
or by using the &
operator.
返回一个新集合,该集合具有该集合和other
集合共同的元素。 它是由intersection()
或使用&
运算符执行的。
Syntax
句法
intersection(*others)
intersection ( *others )
set & other & ...
set & other & ...
Example 1: Find the intersection of two sets — A
and B
示例1:找到两个集合的交集— A
和 B
It’ll return a new set containing common elements from the set A
and set B
.
它将返回一个新集合,其中包含集合A
和集合B
中的公共元素。
A={1,2,3,4,5}
B={2,4,6,8}#intersection is performed by intersection() method or & operatorprint (A.intersection(B))#Output:{2,4}print (A&B)#Output:{2,4}
Example 2: Find the intersection of more than two sets
示例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}
Difference between the intersection()
method and the &
operator:
intersection()
方法和&
运算符之间的区别:
intersection()
: It’ll accept any iterable as an argumentintersection()
:它将接受任何可迭代的参数&
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.&
运算符:仅接受set作为参数。 否则,它将引发TypeError
。
Example 3: Giving iterable
as an argument in the intersection()
method
示例3: 在 intersection()
方法 中将 iterable
用作参数
A={1,2,3,4,5}#iterable is given as listprint (A.intersection([1,4,6]))#Output:{1,4}
#iterable is given as tupleprint (A.intersection((2,4,6)))#Output:{2,4}
#iterable is given as range objectprint (A.intersection(range(5,10)))#Output:{5}
#iterable is given as a dictionaryprint (A.intersection({1:'a','b':7}))#Output:{1}
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'
'intersection_update()' (’intersection_update()’)
It updates the set, keeping only elements found in it and the other
. It’s performed by using intersection_update()
or by using the&=
operator. The return type is None
. It’ll modify the original set itself.
它更新集合,只保留其中的元素和other
。 它是通过使用intersection_update()
或使用&=
运算符来执行的。 返回类型为None
。 它将修改原始集本身。
Syntax
句法
intersection_update(*others)
intersection_update ( *others )
set &= other & …
set &= other & …
Example 1: Find the intersection_update()
between two sets — A
and B
示例1:找到 两个集合 A
和 B
之间 的 intersection_update()
It’ll update set A
by keeping only the elements found in both of the sets.
通过仅保留在两个集合中找到的元素,它将更新集合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&=B
print (A) #Output: {4,5}
'区别()' (‘difference()’)
Returns a new set with elements in the set that aren’t in the other
. It’s performed by difference()
or by using the -
operator.
返回一个新集合,该集合中的元素不在other
集合中。 它是通过difference()
或使用-
运算符执行的。
Syntax
句法
difference(*others)
difference ( *others )
set - other - ...
set - other - ...
Example 1: Find the difference between two sets — A
and B
示例1:找出两组之间的差异— A
和 B
It’ll return a new set containing elements from set A
not in set B.
它将返回一个新集合,其中包含集合A
不在集合A
中的元素。
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}
Example 2: Find the difference between more than two sets
示例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 between the difference()
method and the -
operator:
Difference difference()
方法和-
运算符之间的difference()
:
difference()
: It’ll accept any iterable as an argumentDifference
difference()
:它将接受任何可迭代的参数-
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.-
运算符:它将仅接受set作为参数。 否则,它将引发TypeError
。
Example 3: Giving iterable
as an argument in the difference()
method
例3: 在 difference()
方法 中将 iterable
作为参数
A={1,2,3,4,5}#iterable is given as listprint (A.difference([1,2,3]))#Output:{4,5}
#iterable is given as tupleprint (A.difference((1,2,3)))#Output:{4,5}
#iterable is given as range objectprint (A.difference(range(1,4)))#Output:{4,5}
#iterable is given as a dictionaryprint (A.difference({1:'a',2:'b',3:'c'}))#Output:{4,5}
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'
'difference_update()' (‘difference_update()’)
Removes the element from the set that’s also present in the other
set. It’s performed by using the -=
operator or by using the difference_update()
method. The return type is None
. It’ll modify the original set itself.
从other
集合中也存在的集合中删除元素。 通过使用-=
运算符或使用difference_update()
方法来执行。 返回类型为None
。 它将修改原始集本身。
Syntax
句法
difference_update(*others)
difference_update ( *others )
set -= other | ...
set -= other | ...
Example 1: Find the difference_update()
between two sets — A
and B
示例1:找到 两个集合 A
和 B
之间 的 difference_update()
It’ll update set A
by removing elements that are also present in set B
.
它将通过删除集合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 setprint (A) #Output: {1,3,5}
# difference_update by using -= operatorA-=(B)
print (A) #Output: {1,3,5}
Example 2: Find the difference_update
between more than two sets
示例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 setprint (A) #Output: {3}
# difference_update by using -= operatorA={1,2,3}
B={1}
C={2}
A-=B|C
print (A) #Output: {3}
Difference between the difference_update()
method and the -=
operator:
Difference_update difference_update()
方法和-=
运算符之间的difference_update()
:
difference_update()
: It’ll accept any iterable as an argumentDifference_update
difference_update()
:它将接受任何可迭代的参数-= operator
: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
.-= operator
:它将仅接受set作为参数。 否则,它将引发TypeError
。
Example 3: Giving iterable
as an argument in the difference_update()
method
示例3: 在 difference_update()
方法 中将 iterable
作为参数
#iterable is given as listA={1,2,3}
B=[1]
print (A.difference_update(B))#Output:Noneprint (A)#Output:{2,3}
Example 4: Giving iterable
as an argument for -=
operator
示例4: 为 -=
运算符 提供 iterable
作为参数
A={1,2,3}
B=[1]
A-=B
print (A) #Output: TypeError: unsupported operand type(s) for -=: 'set' and 'list'
'symmetric_difference()' (‘symmetric_difference()’)
Return a new set with elements in either the set or other
but not both. It’s performed by symmetric_difference()
or by using the ^
operator.
返回一个新集合,该集合中有一个或other
元素,但不包含两个元素。 它是由symmetric_difference()
或使用^
运算符执行的。
Syntax
句法
symmetric_difference(other)
symmetric_difference ( other )
set ^ other
set ^ other
Example 1: Find the symmetric_difference
between two sets — A
and B
示例1:找到 两组 A
和 B
之间 的 symmetric_difference
It’ll return a new set containing elements from either set A
and set B
but not elements found in both sets.
它将返回一个新集合,其中包含来自集合A
和集合B
元素,但不包含在两个集合中都找到的元素。
A={1,2}
B={2,3}print (A.symmetric_difference(B))#Output:{1,3}print (A^B)#Output:{1,3}
Example 2: symmetric_difference
is only performed between two sets
示例2: symmetric_difference
差异仅在两组之间执行
The symmetric_difference() method
isn’t supported by multiple sets. If more than two sets are given, it’ll raise a TypeError
.
多个集不支持symmetric_difference() method
。 如果给出两个以上的集合,它将引发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)
But we can find the symmetric_difference
of multiple sets using ^
.
但是我们可以使用^
找到多个集合的symmetric_difference
。
A={1,2}
B={2,3,5}
C={3,4}
print (A^B^C)#Output:{1,4,5}
The difference between the symmetric_difference
method and the&
operator:
symmetric_difference
方法和&
运算符之间的区别:
symmetric_difference()
: It’ll accept any iterable as an argument. This method doesn’t allow for multiple sets.symmetric_difference()
:它将接受任何可迭代的参数。 此方法不允许使用多个集合。^
operator: It’ll accept only a set as an argument. Otherwise, it’ll raise aTypeError
. By using the^
operator, you can find thesymmetric_difference
between multiple sets.^
运算符:它仅接受set作为参数。 否则,它将引发TypeError
。 通过使用^
运算符,您可以找到多个集合之间的symmetric_difference
。
Example 3: Giving iterable
as an argument in the symmetric_difference
method
示例3: 在 symmetric_difference
方法 中将 iterable
作为参数
#iterable is given as listA={1,2,3}
B=[1]
print (A.symmetric_difference(B))#Output:{2,3}
#iterable is given as tupleA={1,2,3}
B=(1,)
print (A.symmetric_difference(B))#Output:{2,3}
#iterable is given as range objectA={1,2,3}
B=range(2)
print (A.symmetric_difference(B))#Output:{2,3}
Example 4: Giving iterable
as an argument for the ^
operator
示例4: 为 ^
运算符 提供 iterable
作为参数
A={1,2,3}
B=[1]
A^B
print (A) #Output: TypeError: unsupported operand type(s) for ^: 'set' and 'list'
'symmetric_difference_update()' (‘symmetric_difference_update()’)
Updates the set, keeping only elements found in either set but not in both. It’s performed by using symmetric_difference_update()
or by using the ^=
operator. The return type is None
. It’ll modify the original set itself.
更新集合,仅保留在两个集合中均找到的元素,而不同时在两个集合中均找到。 通过使用symmetric_difference_update()
或使用^=
运算符来执行。 返回类型为None
。 它将修改原始集本身。
Syntax
句法
symmetric_difference_update(other)
symmetric_difference_update ( other )
set ^= other
set ^= other
Example 1: Find the symmetric_difference_update()
between two sets — A and B
示例1: 在两组A和B之间 找到 symmetric_difference_update()
It will update set A
by keeping only elements found in either set but not in both.
它将通过仅保留在任一集合中找到但不在两个集合中找到的元素来更新集合A
#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^=B
print (A) #Output: {1, 2, 3, 6, 7, 8}
'isdisjoint()' (‘isdisjoint()’)
Returns True
if the set has no elements in common with the other
. Sets are disjointed if and only if their intersection is the empty set.
如果集合没有与other
集合相同的元素,则返回True
。 当且仅当它们的交集为空集时,集才脱节。
Syntax
句法
isdisjoint(other)
isdisjoint ( other )
Example
例
#Set A and Set B containing common elementsA={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 elementsA={1,2}
B={3,4}
print (A.isdisjoint(B))#Output:True
'issubset()' (‘issubset()’)
Test whether every element in the set is in other
.
测试集合中的每个元素是否都在other
。
Syntax:
句法:
issubset(other)
issubset ( other )
set <= other
set <= other
Example: Check whether set A
is a subset of B
示例:检查集合 A
是否 为 B
的子集
Can be done by the issubset()
method or by using the ≤
operator.
可以通过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: False
Proper subset
适当的子集
Test whether the set is a proper subset of other
— that is, set <= other and set != other
.
测试set是否为other
的适当子集,即set <= other and set != other
。
Syntax
句法
set < other
set < other
Example: Check whether set A
is a proper subset of B
示例:检查集合 A
是否 是 B
的适当子集
If both sets are equal, this means A.issubsetset(B)
returns True
. But the proper subset A<B
will return False
.
如果两个集合相等,则意味着A.issubsetset(B)
返回True
。 但是适当的子集A<B
将返回False
。
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A<B)#Output: FalseA={1,2,3,4,5}
B={1,2,3,4,5}
print (A<B)#Output: FalseA={1,2,3}
B={1,2,3,4,5}
print (A<B)#Output: True
'issuperset()' (‘issuperset()’)
Test whether every element in other
is in the set.
测试other
元素是否在集合中。
Syntax
句法
issuperset(other)
issuperset ( other )
set >= other
set >= other
Example: Check whether set A
is a superset of B
示例:检查集合 A
是否 为 B
的超集
Can be done by theissuperset()
method or by using the ≥
operator.
可以通过issuperset()
方法或使用≥
运算符来完成。
A={1,2,3,4,5}
B={4,5,6,7,8}
print (A.issuperset(B)) #Output: Falseprint (A>=B)#Output:TrueA={1,2,3,4,5}
B={1,2,3}
print (A.issuperset(B)) #Output: Trueprint (A>=B)#Output:True
Proper superset
适当的超集
Test whether the set is a proper superset of other
— that is, set >= other and set != other
.
测试集合是否为other
的适当超集-即, set >= other and set != other
。
Syntax
句法
set > other
set > other
Example: Check whether set A
is a proper superset of B
.
示例:检查集合 A
是否 为 B
的适当超集 。
If both sets are equal, it means A.issuperset(B)
returns True
. But the proper superset A>B
will return False
.
如果两个集合相等,则意味着A.issuperset(B)
返回True
。 但是适当的超集A>B
将返回 False
。
A={1,2,3,4,5}
B={4,5}
print (A>B)#Output: TrueA={1,2,3,4,5}
B={1,2,3,4,5}
print (A>B)#Output: FalseA={1,2,3}
B={1,2,3,4,5}
print (A>B)#Output: True
注意 (Note)
All update methods are not supported by frozenset
. The frozenset
type is immutable and hashable — its contents can’t be altered after it’s created. Since all update methods modify the original set, it’s not supported by frozenset
.
frozenset
不支持所有更新方法。 该frozenset
类型是不可改变的, 可哈希 -它的创建后,其内容不能被改变。 由于所有更新方法都修改了原始集,所以frozenset
不支持frozenset
。
结论 (Conclusion)
Mathematical set operations can be performed in two ways: by using an operator or by using a method.
可以通过两种方式执行数学设置操作:使用运算符或使用方法。
The difference is if we use methods, they’ll accept iterable
as an argument. But for operators, arguments should be set only. If not, it’ll raise a TypeError
. All update methods will update the original set, and it’s not supported in frozenset
. Other than update methods, all other methods return a new set.
区别在于,如果我们使用方法,它们将接受 iterable
作为参数。 但是对于运算符,仅应设置参数。 如果没有,它将引发TypeError
。 所有更新方法都将更新原始集合,而且frozenset
不支持frozenset
。 除了更新方法外,所有其他方法都返回一个新集合。
I hope you have found this article helpful — thanks for reading!
希望本文对您有所帮助-感谢您的阅读!
资源资源 (Resources)
翻译自: https://medium.com/better-programming/mathematical-set-operations-in-python-e065aac07413
python 集合运算