set.intersection_update() 交集(原地)
最后更新:2026-09-22
set.intersection_update() 交集(原地)
set.intersection_update(*others) 把 set 与 others 的交集写入 set。
| 分类 | 集合方法 |
|---|---|
| 类型 | 内建类型方法 |
| Python 版本 | all |
📝 语法
set.intersection_update(*others)
⚙️ 参数
| others 可选 | 零个或多个可迭代对象,可省略。 |
|---|
返回值:None(原地只保留与 all others 的公共元素)。
是否修改原对象:是(原地修改)
▶ 示例
编辑下方代码,点击运行,在输出面板查看结果:
运行结果:
{2, 3}
💡 相关案例
更多实战案例即将上线,敬请期待。
❓ 常见问题
Qintersection_update() 做什么?
A原地把集合变成它与 all others 的交集,返回 None。与 intersection()(返回新集合)正好相反。
Q没有公共元素会怎样?
A集合被清空为 set(),不会报错。
Q与 &= 的区别?
A等价,但 &= 要求右侧是 set;intersection_update() 接受任意可迭代对象。