python intersection_update()方法

intersection_update()方法

intersection_update()方法,Python集合内置实例方法,可用于获取两个或两个以上集合中共同的元素,即求多个集合的交集,与上一节中介绍的intersection()方法返回新集合set的方式不同,intersection_update()方法是修改原集合,即移除原集合中与参数指定的集合不重叠的元素。


intersection_update()语法及参数

set.intersection_update(set1[set2 ... setN])

提示:一般情况下,参数set1为必须参数(如果不传递参数,返回一个与原集合元素相同的新集合),set2至setN的集合为可选参数。


intersection_update()返回值

一般情况下,无返回值,但intersection_update()会修改调用对象集合。如果不传递参数,返回值为一个与原集合元素相同的新集合。


intersection_update()实例代码

>>> set1 = {1,2,3}
>>> set2 = {2,1,5}
>>> set1.intersection_update(set2)
>>> set1 #intersection_update()方法修改原集合
{1, 2}
>>> set3 = {1,6}
>>> set1.intersection_update(set2,set3)
>>> set1
{1}
>>> set1.intersection_update()
>>> set1
{1}
>>> id(set1.intersection_update()) #不传递参数,intersection_update()返回值的内存地址与原集合不同
216689
>>> id(set1)
216698

全栈后端 / python教程 :


























Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.