小白教程
所有教程
关于
Search
172.69.7.132
172.69.7.132
参数设置
贡献
退出
操作
编辑
移动
保护
信息
历史
删除
查看“Ref set union”的源代码
本页内容
上一节:
Ref_set_symmetric_difference_update
下一节:
Ref_set_update
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
{{DISPLAYTITLE:union()}}[[Category:python3 set|16]] = Python Set union() 方法 = [[python3 set#setmethod|Python 集合]] == 描述 == union() 方法返回两个集合的并集,即包含了所有集合的元素,重复的元素只会出现一次。 == 语法 == union() 方法语法: <sample title="" desc="" lang="python" hererun="1"> set.union(set1, set2...) </sample> == 参数 == * set1 -- 必需,合并的目标集合 * set2 -- 可选,其他要合并的集合,可以多个,多个使用逗号 , 隔开。 == 返回值 == 返回一个新集合。 合并两个集合,重复元素只会出现一次: == 实例 1 == <sample title="" desc="" lang="python" hererun="1"> x = {"apple", "banana", "cherry"} y = {"google", "xiaobai", "apple"} z = x.union(y) print(z) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> {'cherry', 'xiaobai', 'google', 'banana', 'apple'} </sample> 合并多个集合: == 实例 1 == <sample title="" desc="" lang="python" hererun="1"> x = {"a", "b", "c"} y = {"f", "d", "a"} z = {"c", "d", "e"} result = x.union(y, z) print(result) </sample> 输出结果为: <sample title="" desc="" lang="python" hererun="1"> {'c', 'd', 'f', 'e', 'b', 'a'} </sample> [[python3 set#setmethod|Python 集合]]
返回至“
Ref set union
”。
上一节:
Ref_set_symmetric_difference_update
下一节:
Ref_set_update