在Python中,可以使用union()方法或者|运算符来实现set的并集操作。
union()
|
示例:
# 定义两个set set1 = {1, 2, 3, 4} set2 = {3, 4, 5, 6} # 使用union()方法 result1 = set1.union(set2) print(result1) # 输出:{1, 2, 3, 4, 5, 6} # 使用|运算符 result2 = set1 | set2 print(result2) # 输出:{1, 2, 3, 4, 5, 6}