定义和用法
intersection_update() 方法会删除各集合中都不存在的项目。
intersection_update() 方法与 intersection() 方法不同,因为 intersection() 方法返回一个新集合,其中没有不需要的项目,而 intersection_update() 方法从原始集中删除了不需要的项目。
实例
例子1
删除集合 x 和集合 y 都不存在的项目:
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.intersection_update(y) print(x)
运行实例 »
例子 2
比较 3 个集合,返回的集合包含存在于所有 3 个集合中的项目:
x = {"a", "b", "c"} y = {"c", "d", "e"} z = {"f", "g", "c"} x.intersection_update(y, z) print(x)
语法
set.intersection_update(set1, set2 ... etc)
参数
返回:Python 集合