设置difference_update()方法 (Set difference_update() Method)
difference_update() method is used to update the set with the elements of this (original) set (set1) by removing the elements from another set (set2) that do not exist in the set1 and repeated elements.
difference_update()方法用于通过从另一个集合(set2)中删除set1中不存在的元素和重复的元素来使用此(原始)集合(set1)的元素更新集合。
Syntax:
句法:
set_name1.difference_update(set_name2)
Parameter(s):
参数:
set_name2 – Name of the another/second set to find the difference with set_name1.
set_name2 –另一个/第二个集合的名称,用于查找与set_name1的差异。
Return value:
返回值:
The return type of this method is <class 'NoneType'>, it returns nothing.
此方法的返回类型为<class'NoneType'> ,它什么也不返回。
Example 1:
范例1:
# Python Set difference_update() Method with Example
# declaring the sets
cars_1 = {"Porsche", "Audi", "Lexus"}
cars_2 = {"Porsche", "Mazda", "Lincoln"}
# printing the sets before difference_update() call
print("cars_1:", cars_1)
print("cars_2:", cars_2)
# difference_update() method call
cars_1.difference_update(cars_2)
# printing the sets after difference_update() call
print("cars_1:", cars_1)
print("cars_2:", cars_2)
Output
输出量
cars_1: {'Audi', 'Lexus', 'Porsche'}
cars_2: {'Lincoln', 'Mazda', 'Porsche'}
cars_1: {'Audi', 'Lexus'}
cars_2: {'Lincoln', 'Mazda', 'Porsche'}
See the output, cars_1 is updated with the unique elements by removing other elements that exist in cars_2 and repeated element "Porsche".
看到输出,cars_1与独特的元素通过除去存在于cars_2和重复的元素“保时捷”的元件更新。
Example 2:
范例2:
# Python Set difference_update() Method with Example
# declaring the sets
x = {"ABC", "PQR", "XYZ"}
y = {"ABC", "PQR", "XYZ"}
z = {"DEF", "MNO", "XYZ"}
# printing the results
print("Before difference_update()...")
print("x:", x)
print("y:", y)
print("z:", z)
# printing the differences
x.difference_update(y)
y.difference_update(z)
z.difference_update(x)
# printing the results
print("After difference_update()...")
print("x:", x)
print("y:", y)
print("z:", z)
Output
输出量
Before difference_update()...
x: {'ABC', 'XYZ', 'PQR'}
y: {'ABC', 'XYZ', 'PQR'}
z: {'MNO', 'XYZ', 'DEF'}
After difference_update()...
x: set()
y: {'ABC', 'PQR'}
z: {'MNO', 'XYZ', 'DEF'}
翻译自: https://www.includehelp.com/python/set-difference_update-method-with-example.aspx