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)
运行示例»