Python 集合 intersection_update() 方法
實例
刪除集合 x 和集合 y 都不存在的項目:
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.intersection_update(y) print(x)
定義和用法
intersection_update() 方法會刪除各集合中都不存在的項目。
intersection_update() 方法與 intersection() 方法不同,因為 intersection() 方法返回一個新集合,其中沒有不需要的項目,而 intersection_update() 方法從原始集中刪除了不需要的項目。
語法
set.intersection_update(set1, set2 ... etc)
參數值
參數 | 描述 |
---|---|
set1 | 必需。要在其中檢索相等項目的集合。 |
set2 |
可選。要在其中檢索相等項目的另一集合。 您能夠比較任意多的集合。 集合由逗號分隔。 |
更多實例
實例
比較 3 個集合,返回的集合包含存在于所有 3 個集合中的項目:
x = {"a", "b", "c"} y = {"c", "d", "e"} z = {"f", "g", "c"} x.intersection_update(y, z) print(x)