设置交集更新()方法 (Set intersection_update() Method)
intersection_update() method is used to update the original set with the common elements which exist in all set i.e. we can say intersection_update() is used to remove unwanted elements (which are not available in all sets).
junction_update()方法用于使用所有集合中都存在的公共元素来更新原始集合,也就是说,可以说intersection_update()用于删除不需要的元素(并非在所有集合中都可用)。
Syntax:
句法:
set1. intersection_update(set1, set2, set3, ...)
Parameter(s):
参数:
set1 – It represents the set to be compared with this set.
set1 –代表要与此集合进行比较的集合。
set2, set3, ... – These are optional sets, we can provide multiple sets to be compared.
set2,set3,... –这些是可选集,我们可以提供多个要比较的集。
Return value:
返回值:
The return type of this method is <class 'NoneType'>, it returns nothing.
此方法的返回类型为<class'NoneType'> ,它什么也不返回。
Example 1:
范例1:
# Python Set intersection_update() Method with Example
# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
# before method call
print("Before intersection_update() method call...")
print("cars_1:", cars_1)
print("cars_2:", cars_2)
# intersection_update() method call
cars_1.intersection_update(cars_2)
# printing the set after method call
print("After intersection_update() method call...")
print("cars_1:", cars_1)
print("cars_2:", cars_2)
Output
输出量
Before intersection_update() method call...
cars_1: {'Lexus', 'Porsche', 'Audi'}
cars_2: {'Lincoln', 'Porsche', 'Mazda'}
After intersection_update() method call...
cars_1: {'Porsche'}
cars_2: {'Lincoln', 'Porsche', 'Mazda'}
Example 2:
范例2:
# Python Set intersection_update() Method with Example
# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "ABC"}
# printing the results
print("x:", x)
print("y:", y)
print("z:", z)
# printing the common elements
x.intersection_update(y,z)
print("x: ",x)
Output
输出量
x: {'XYZ', 'PQR', 'ABC'}
y: {'XYZ', 'PQR', 'ABC'}
z: {'MNO', 'ABC', 'DEF'}
x: {'ABC'}
翻译自: https://www.includehelp.com/python/set-intersection_update-method-with-example.aspx