Python Set union() 方法

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

描述

union() 方法返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。

语法

union() 方法语法:

set.union(set1, set2...)

参数

  • set1 -- 必需,合并的目标集合
  • set2 -- 可选,其他要合并的集合,可以多个,多个使用逗号 , 隔开。

返回值

返回一个新集合。

实例

合并两个集合,重复元素只会出现一次:

实例 1

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

输出结果为:

{'cherry', 'codercto', 'google', 'banana', 'apple'}

合并多个集合:

实例 1

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

输出结果为:

{'c', 'd', 'f', 'e', 'b', 'a'}

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

查看所有标签

Introduction to Computer Science Using Python

Introduction to Computer Science Using Python

Dierbach, Charles / 2012-12 / $ 133.62

Introduction to Computer Science Using Python: A Computational Problem-Solving Focus introduces students to programming and computational problem-solving via a back-to-basics, step-by-step, objects-la......一起来看看 《Introduction to Computer Science Using Python》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具