python 中的交集_update()
函數有助于集合更新。它首先找出給定集合的交集。并用集合交集的結果元素更新第一個集合。集合交集給出了包含所有給定集合中公共元素的新集合。
**A.intersection_update(*other_sets)** #where A is a set of any iterables, like strings, lists, and dictionaries
交集 _ 更新()參數:
intersection_update()
函數可以采用許多集合參數進行比較(*表示),并用逗號分隔這些集合。
參數 | 描述 | 必需/可選 |
---|---|---|
A | 在中搜索相等項目并更新的集合 | 需要 |
其他集 | 另一組用于搜索相等的項目 | 可選擇的 |
交集 _ 更新()返回值
此方法不返回值。它會更新原始設置。
結果= A.intersection_update(B,C)考慮這一點,這里的結果將是無。A 將是 A、B、c 的交匯點,而 B & C 保持不變。
Python 中的交集_update()
方法示例
示例 intersection _ update()
在 Python 中是如何工作的?
A = {'a', 'b', 'c', 'd'}
B = {'c', 'e', 'f', 'a'}
result = A.intersection_update(B)
print('result =', result)
print('A =', A)
print('B =', B)
輸出:
result = None
A = {'a', 'c'}
B = {'c', 'e', 'f', 'a'}
示例 2:帶有兩個參數的 Python 方法交集_update()
A = {'a', 'b', 'c', 'd'}
B = {'c', 'e', 'f', 'a'}
C = {'d', 'f', 'a', 'g'}
result = C.intersection_update(B, A)
print('result =', result)
print('C =', C)
print('B =', B)
print('A =', A)
輸出:
result = None
C = {'a'}
B = {'c', 'e', 'f', 'a'}
A = {'a', 'b', 'c', 'd'}