intersection_update()使用集的交集更新調(diào)用intersection_update()方法的集。
兩個(gè)或更多集合的交集是所有集合共有的元素集合。
要了解更多信息,請(qǐng)?jiān)L問(wèn)Python set Intersection。
junction_update()的語(yǔ)法為:
A.intersection_update(*other_sets)
intersection_update()允許任意數(shù)量的參數(shù)(集合)。
注意: *不是語(yǔ)法的一部分。用于指示該方法允許任意數(shù)量的參數(shù)。
此方法返回None(意味著,沒(méi)有返回值)。它僅更新調(diào)用intersection_update()方法的集合。
假設(shè),
result = A.intersection_update(B, C)
當(dāng)您運(yùn)行代碼時(shí),
result 將為 None
A 等于A B和C的交點(diǎn)
B 保持不變
C 保持不變
A = {1, 2, 3, 4}
B = {2, 3, 4, 5}
result = A.intersection_update(B)
print('result =', result)
print('A =', A)
print('B =', B)運(yùn)行該程序時(shí),輸出為:
result = None
A = {2, 3, 4}
B = {2, 3, 4, 5, 6}A = {1, 2, 3, 4}
B = {2, 3, 4, 5, 6}
C = {4, 5, 6, 9, 10}
result = C.intersection_update(B, A)
print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)運(yùn)行該程序時(shí),輸出為:
result = None
C = {4}
B = {2, 3, 4, 5, 6}
A = {1, 2, 3, 4}