Python/ref set intersection update
来自菜鸟教程
<languages />
Python设置交集_更新()方法
例
删除两个都不存在的项目
x
and
y
x =
{"apple", "banana", "cherry"}
y = {"google",
"microsoft", "apple"}
x.intersection_update(y)
print(x)
定义和用法
The
intersection_update()
方法删除两个集合(如果在两个以上集合之间进行比较,则不在所有集合中)中不存在的项目。
The
intersection_update()
方法不同于
intersection()
方法,因为
intersection()
方法
returns a new set
,没有多余的物品,并且
intersection_update()
方法
removes
原始集中不需要的项目。
句法
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)