Python Set intersection_update() 方法

Python 3 教程 · 2019-02-06 19:28:44

描述

intersection_update() 方法用于获取两个或更多集合中都重叠的元素,即计算交集。

intersection_update() 方法不同于 intersection() 方法,因为 intersection() 方法是返回一个新的集合,而 intersection_update() 方法是在原始的集合上移除不重叠的元素。

语法

intersection_update() 方法语法:

set.intersection_update(set1, set2 ... etc)

参数

  • set1 -- 必需,要查找相同元素的集合
  • set2 -- 可选,其他要查找相同元素的集合,可以多个,多个使用逗号 , 隔开

返回值

无。

实例

返回一个新集合,该集合的元素既包含在集合 x 又包含在集合 y 中:

实例 1

x = {"apple", "banana", "cherry"} y = {"google", "codercto", "apple"} x.intersection_update(y) print(x)

输出结果为:

{'apple'}

计算多个集合的并集:

实例 1

x = {"a", "b", "c"} y = {"c", "d", "e"} z = {"f", "g", "c"} x.intersection_update(y, z) print(x)

输出结果为:

{'c'}

点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html

查看所有标签

Introduction to Programming in Java

Introduction to Programming in Java

Robert Sedgewick、Kevin Wayne / Addison-Wesley / 2007-7-27 / USD 89.00

By emphasizing the application of computer programming not only in success stories in the software industry but also in familiar scenarios in physical and biological science, engineering, and appli......一起来看看 《Introduction to Programming in Java》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具