可以使用 union() 方法或 | 操作符来合并两个集合。

示例代码:

set1 = {1, 2, 3}
set2 = {3, 4, 5}

# 使用 union() 方法合并两个集合
set3 = set1.union(set2)
print(set3)  # 输出:{1, 2, 3, 4, 5}

# 使用 '|' 操作符合并两个集合
set4 = set1 | set2
print(set4)  # 输出:{1, 2, 3, 4, 5}

注意:合并后的集合中不会有重复元素。

Python 合并集合:使用 union() 或 '|' 操作符

原文地址: https://www.cveoy.top/t/topic/otjQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录